Workshop - words Solitaire

we used to play idiom Solitaire game, we try Solitaire English bar: 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

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

Apple

Elephant

Tree

unified input file name: input1.txt , input2.txt

unified output file name: output1.txt , output2.txt

program needs 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 words 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?

Requirements will design, code implementation, to achieve screenshots, personal summary published in the form of blog posts.

Design ideas:

     The word list stored into the container, after the first acquisition of the first word of the first, the last letter, then the recycle and last letter of the next word acquired, so that the last letter of the first word and the second word is the last letter the first letter of the comparison, if the same, then the first letter of the second word is assigned to the first word of the variable, and so on.

Experiment code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Text_2 {
static int N=5;
static String b[]=new String[500];
public static void main(String[] args) 
    {
        
        
        String sz=writeFromFile.readTxtFile("input3.txt");
        String ltxt=null;
        if(sz==null)
        {
            System.out.println("Find the file specified" ); 
        } 
        the else  IF (SZ == "Kong" ) 
        { 
            System.out.println ( "file is empty!" );   
        } 
        
        The else 
        { 
            System.out.println (ltxt = StatList1 (SZ )); 
            
            SELECT (B); 
        } 
        
        the try { 
        writeFromFile.daochu (ltxt); 
        } the catch (IOException E) {
         // the TODO Auto-Generated Block the catch 
        e.printStackTrace (); 
        } 
        
        

    } 
public  static  int woor (String A) 
    {
        int n=0;
        File ctoFile = new File("stopword.txt");
        InputStreamReader rdCto;
        try {
            rdCto = new InputStreamReader(new FileInputStream(ctoFile));
            BufferedReader bfReader = new BufferedReader(rdCto);
            String txtline = null;
            try {
                while ((txtline = bfReader.readLine()) != null) 
                {
                    if(txtline.equals(a))
                        {
                            n=1;
                        }
                }
                bfReader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return n;
    }
public static ArrayList<String> getFiles(String path) {
        ArrayList<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                files.add(tempList[i].toString());
            }
            if (tempList[i].isDirectory()) {
            }
        }
        return files;
    }
public static String StatList1(String str) {
        StringBuffer sb = new StringBuffer();
        HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表
        String[] slist = str.split("[^a-zA-Z\']+");
        for (int i = 0; i < slist.length; i++) 
        {
            if (!has.containsKey(slist[i])) 
            { 
                   has.put(slist[i], 1);
            } 
            else 
            {
                   has.put(slist[i],has.get(slist[i])+1 );
            }
        }
        Iterator<String> iterator = has.keySet().iterator();
        String a[]=new String[500];
        int s[]=new int[500];
        int judge;
        int n=400;
        for(int i=0;i<n;i++)
        {
            iterator = has.keySet().iterator();
            while(iterator.hasNext())
            {
                String word = (String) iterator.next();
                if(s[i]<has.get(word))
                {
                    s[i]=has.get(word);
                    a[i]=word;
                    b[i]=word;
                }
             }
            judge=woor(a[i]);
            if(judge==1)
            {
                n++;
                has.remove(a[i]);
            }
            else
            {
//                System.out.println(a[i]+"6666");
//             sb.append("单词:").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n");
             has.remove(a[i]);
            }
        }
             return sb.toString();
        }
public static void select(String b[])
    {
        List<Object> list=new ArrayList<>();
        int q=0;
        int n0=0;
        for(int i=0;i<b.length;i++)
        {
            if(b[i]!=null)
            {
                n0++;
            }
        }
        if(n0==1)
        {
            System.out.println("只有一个单词");
        }
        else
        {
            String c="and";
            q=c.length();
            char n1=c.charAt(0);
            char n2=c.charAt(q-1);
            list.add(c);
            for(int j=0;j<b.length;j++)
            {
                String w=b[j];
                if(w!=null)
                {
                    int e=w.length();
                    char n3=w.charAt(0);
                    char n4=w.charAt(e-1);
                    if(n2==n3)
                    {
                        list.add(w);
                        n1=n3;
                        n2=n4;
                    }
                }
                
            }
            String l=null;
            if(list.size()==1)
            {
                System.out.println("没有首尾相连");
            }
            else
            {
            for(int i=0;i<list.size();i++)
            {
                l=l+list.get(i)+"->";
                System.out.println(list.get(i)+"->");
            }
            try {
                daochu(l);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
        }
        
        
    }
    public static void daochu(String a) throws IOException
    {
        File file =new File("output1.txt");
        Writer out =new FileWriter(file);
        String data=a;
        out.write(data);
        out.close();

    }


    
    


}

Screenshot experiment:

Guess you like

Origin www.cnblogs.com/liujinxin123/p/10994047.html
Recommended