Idiom Solitaire game we used to play, we try it in English Solitaire: a text file has N different English words, we could write a program to quickly find the longest chain of English words can be connected end to end, each word can only be used at most once. The longest definition is: the largest number regardless of the number of words and letters in a word.
Unified enter the file name: input1.txt, input2.txt
Unified output file name: output1.txt, output2.txt
Programs need to consider the following anomalies:
For example, the file does not exist, your program will crash it, or to gracefully exit and prompt information to the user?
If the file does not have any word, only one word, no word can be connected end to end, how the program should output?
If the input file has ten thousand words, how fast your program can output the results?
Experiments source
package 数组; import java.awt.geom.Area; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.OutputStream; public class dancilian { public static void main(String[] args) { // TODO Auto-generated method stub //String[] A={"apple","zoo","elephant","under","fox","dog","moon","leaf","tree"}; dancilian a=new dancilian(); File file = new File("E:/input1.txt"); String b=a.read(file); String[] A =b.split(" "); String[] f; String[] l; String[] end; f=a.first(A); l=a.last(A); end=a.jielong(A, l, f); for(int i=0;i<3;i++) System.out.println(end[i]); try { a.write(end); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String[] first(String[] A)//返回首字母数组 { String[] F= new String[A.length]; for(int i=0;i<A.length;i++) { F[i]=A[i].substring(0,1); } return F; } public String[] last(String[] A)//返回尾字母数组 { String[] L= new String[A.length]; for(int i=0;i<A.length;i++) { L[i]=A[i].substring(A[i].length()-1); } return L; } public String[] jielong(String[] A,String[] L,String[] F)//获取接龙字母 { String[] end=new String[A.length]; int k=0; end[0]=A[0]; for(int j=0;j<A.length;j++) { if((end[k].substring(end[k].length()-1)).equals(F[j])) { //System.out.println(end[k].substring(end[k].length()-1)); end[++k]=A[j]; } } return end; } Public String Read (File File) { // read element the StringBuilder Result = new new the StringBuilder (); the try { BufferedReader br = new new BufferedReader ( new new the FileReader (File)); // configured to read a class file BufferedReader String s = null ; the while (! (br.readLine S = ()) = null ) { // use readLine method, a read line result.append (System.lineSeparator () + S); } br.close (); } the catch ( Exception e) { E .printStackTrace (); } Return result.toString (); } public void Write (String [] STR) throws Exception { // write file // create a text file named on disk d testfile File F = new new File ( "E: / output1.txt " ); // with FileOutputSteam package files, and setting files can be added the OutputStream OUT = new new a FileOutputStream (F, to true ); // character array // String [] = {STR" Shanghai "," Beijing "," Guangdong "," Xiamen "}; for ( int I = 0; I <str.length; I ++ write data to a file ) { out.write (STR [I] .getBytes ()); // out.write ( '\ R & lt'); // \ R & lt \ n-newline out.write ( '\ n-' ); } the out.close () ; // Close the output stream System.out.println ( "successfully written!" ); } }