Idiom (English word chain)

" Construction of the law " exercises

I used to play idiom Solitaire game , 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.

For example, files are:

Apple

Zoo

Elephant

Under

Fox

Dog

Moon

Leaf

Tree

Pseudopseudohypoparathyroidism

 

The longest string of connected English words: apple - elephant - tree, which is output to a file, this is:

              Apple

              Elephant

              Tree

 

Append: Analyzing file does not exist, no word document, a word file, no file associated with (a word file which is a special case)

Remove input.txt text word, the display outout.txt the longest string of connected English words

Task decomposition: first determine whether a file exists

Does not exist -> error

Present -> Remove the word stored in memory -> is determined whether the number of words in a

A -> error

Is not a -> Analyzing connected English words (Length) (determines whether there is a duplicate of the code judgment) -> derived longest English word string connected

All my error output in the console are commented out, change the display in the text

Code:

package 六月六号;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test1 {

	public static void main(String[] args) throws IOException 
	{
		// TODO 自动生成的方法存根
		String filename ="D://dada.txt";
		File  a=new  File(filename);
		File  file=new  File("D://output.txt");
		long startTime = System.currentTimeMillis();
	 //judeFileExists(a);
	 if(judeFileExists(a))
		{
			danci(filename);
		}
	 else
	    { 
		  try {
	        	
        	  FileWriter fw =new FileWriter(file);
        	  fw.write ( "No such file"); 
        	  fw.flush (); 
        	  fw.close (); 
        } 
        the catch (IOException E) { 
	           e.printStackTrace ();       
	       } 
	    } 
	 Long endTime = System.currentTimeMillis (); // Get end time 
	 System.out.println ( "program run time:" + (endTime - startTime) + "ms"); // output program runtime 
	} 

	public static void danci (String S) throws IOException { 
		   
			the BufferedReader br = new new the BufferedReader ( the FileReader new new (S)); 
			the StringBuffer the StringBuffer new new SB = (); 
			String = null text; 
			the while ((text = br.readLine ())! = null) { 
				sb.append (text); // read-out appended to the character stringbuffer 
			} 
			br.close (); // close reading inflow 
			String str = sb.toString () toLowerCase ( );. // Switch to the stringBuffer and lowercase characters 
			String [] words = str.split ( " [^ (a -zA-Z)] + ") ; // non-character segmenting words, all words to obtain 
			the StringBuffer Yao the StringBuffer new new = (); 
			String words B1 = [0]; 
			yao.append (B1); 
			yao.append (" "); 
			//System.out.println(b1); 
			IF (b1.equals (" ")) 
			{ 
				System.out.println (" file is empty "); 
				yao.append (" file is empty "); 
			} 
			the else 
			{ 
				IF (words.length ==. 1) 
				{ 
					System.out.println ( "one word"); 
				} 
			   
			String End = b1.substring (b1.length () -. 1, b1.length ()); 
		   // System.out.println (end); 
		   for (int I =. 1; I <words.length; I ++ ) 
		   {  
			 
		    String start=words[i].substring(0,1);
		    if(end.equals(start))
		     {
		    	if(judechong(yao,words[i]))
		    	{}
		    	else
		    	{
		    	end=words[i].substring(words[i].length()-1,words[i].length());
		    	yao.append(words[i]);
		    	yao.append(" ");
		    	}
		    	
		     }
		    } 
		    
		   if(yao.toString().equals(words[0]+" "))
		    {
				yao.append("无互联语句");
				System.out.println("无互联词");
		    }
		}
			
			
		  // for( String a:words)
		  // {
			 //  System.out.println(a);
		//   }
		 //  System.out.println(yao.toString()); 
		   File file =new File("D://output.txt");
	        try {
	             file.createNewFile();
	        } catch (IOException e) {
	           e.printStackTrace();      
	       }
	      
	        try {
	        	
	        	  FileWriter fw =new FileWriter(file);
	        	  fw.write(yao.toString());
	        	  fw.flush();
	        	  fw.close();
	        }
	        catch (IOException e) {
		           e.printStackTrace();      
		       }
			
	     
	}

public  static boolean  judechong(StringBuffer yao,String word)
{
	String a=yao.toString();
	boolean flag=false;
	String [] words = a.split ( " [^ (a-zA-Z)] +"); // non-character segmenting words, all the words obtained 
	for (int i = 0; i <words.length; i ++ ) 
	{ 
		iF (word.equals (words [I])) 
		{ 
			In Flag = to true; 
		} 
	} 
	return In Flag; 
} 
// determine whether a file exists 
public static Boolean judeFileExists (file file) { 

    iF (File.Exists ()) { 
        the System .out.println ( "file exists"); 
        return to true; 
    } the else { 
        System.out.println ( "file does not exist"); 
        // the try { 
        // file.createNewFile (); 
       //} the catch (IOException E) { 
       // the TODO Auto-Generated the catch Block 
      // e.printStackTrace ();       
     //}
        return false;
    }
}
}

  

Optimization Analysis:

What if the file is too big to solve:

After opening the file, instead of reading a word a word substitution pattern read all the way out. Then you need to determine the first letter of the word and word spacing of the case. Write two functions, or both together.

Your longest English word string connected may become very long, this time also choose to find a way to write a go.

I can only think of being solved and analyze the problem, what the problem, I hope you propose to solve together.

Guess you like

Origin www.cnblogs.com/gonT-iL-evoL-I/p/10986618.html