Java algorithm problem using depth-first traversal algorithm

Source: Internet
Author: User

Word Solitaire

Word Solitaire is a game similar to the idiom solitaire we often play, and now we know a group of words, and given a beginning letter, asking for the longest "dragon" starting with the letter (each word is up to two occurrences in the "dragon"), and when two words are connected, the coincident part is part of it, for example Beast and Astonish, if the next dragon is changed to Beastonish, the other two adjacent parts can not be included, for example, at and Atide can not be connected.

Input/output format

Input format:

The first behavior of the input is a separate integer n (n<=20) that represents the number of words, the following n lines have one word per line, and the last behavior entered is a single character representing the letter beginning with "Dragon". You can assume that a "dragon" that begins with this letter must exist.

Output format:

Just output the longest length of "dragon" that starts with this letter

Import Java.util.Scanner;
public class JavaApplication1 {
public int length=0;
Public String []str=new string[30];
public int using[]=new int[30];
public int n;
public int repeat (String arr,string brr) {
int min=0;
if (Arr.length () <brr.length ()) {
Min=arr.length ();
}
else{
Min=brr.length ();
}
for (int i = 0; i < min; i++) {
int flag=1;
for (int j = 0; J <= I; j + +) {
if (Arr.charat (Arr.length () -1-i + j)! = Brr.charat (j)) {
Flag = 0;
}
}
if (flag==1) return i+1;
}
return 0;
}
public void process (String arr,int lengthnow) {
if (Length<lengthnow) {
Length=lengthnow;
}
for (int i = 0; i < n; i++) {
if (Using[i] >= 2) continue;
int c = Repeat (arr, str[i]);
if (C > 0) {
using[i]++;
Process (Str[i], Lengthnow + str[i].length ()-c);
using[i]--;
}
}

}
public static void Main (string[] args) {
Scanner s=new Scanner (system.in);
JavaApplication1 java=new JavaApplication1 ();
Java.n=s.nextint ();
for (int i=0;i< java.n;i++) {
Java.str[i]=s.next ();
java.using[i]=0;
}
String Tou=new string (S.next ());
Java.process (Tou,tou.length ());
System.out.println (java.length);
}
}

Java algorithm problem using depth-first traversal algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.