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