计算最长英语单词链

题目:

大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。最长的定义是:最多单词数量,和单词中字母的数量无关。

统一输入文件名称:input1.txt, input2.txt

统一输出文件名称:output1.txt,output2.txt

程序需要考虑下列异常状况:

例如,文件不存在,你的程序会崩溃么,还是能优雅地退出并给用户提示信息?

如果文件没有任何单词、只有一个单词、没有可以首尾相连的单词,程序应该如何输出?

如果输入文件有一万个单词,你的程序能多快输出结果?

设计思想:

首先读取文件,如果文件不存在给用户显示出提示信息,读取文件中的第一个单词,找到单词中的最后一个字母,再进行遍历看哪个单词的第一个字母相同,如果相同再将第二个单词的尾字母赋值给存放第一个单词尾字母的变量,进行循环遍历

package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class output {

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("D:\input.txt");// 读取文件
        if (!file.exists()) {// 如果文件打不开或不存在则提示错误
            System.out.println("您的D盘文件不存在");
            return;
        }else {
            if(file.exists() && file.length() == 0) {  
                System.out.println("您的D盘文件为空!");  
                return;
            }  
        }
        long startTime = System.currentTimeMillis();
        String[] str=new String[1000];
        Scanner x = new Scanner(file);
        int i=0;
        boolean flag=false;
        while(x.hasNextLine()) {
            String[] str=x.nextLine().split("\W+");
            for(int a=0;a<str.length;a++) {
                if(!str[a].equals("")&&str[a].length()>2) {
                    flag=false;
//                    System.out.println(str[a]);
                    if(i!=0) {
                        for(int t=0;t<i;t++) {
                            if(!str[a].equals(str[t])) {
                                flag=true;
                            }
                        }
                    }else {
                        flag=true;
                    }
                    
                    if(flag) {
                        strs[i]=str[a];
                        i++;
                    }
                    
                }
                
            }
        }
        if(i==1) {
            System.out.println("该文件只有一个单词!无法实现词语接龙");
        }
        String sentence = "";
        String word="";
        String max="";
        for(int m=0;m<i;m++) {
            sentence = str[m];
            word = sentence;
            for(int j=m+1;j<i;j++) {
                if(strs[j].toLowerCase().subSequence(0, 1).equals(word.toLowerCase().subSequence(word.length()-1, word.length()))) {
                    word = strs[j];
                    sentence+="   "+word;
                }
            }
            
            if(sentence.indexOf("   ")!=-1) {
                if(sentence.length()>max.length()) {
                    max = sentence;
                }
//                System.out.println(sentence);
            }
            
        }
        FileWriter fw = null;
        try {
        //如果文件存在,则追加内容;如果文件不存在,则创建文件
        File f=new File("D:\output.txt");
        fw = new FileWriter(f, true);
        } catch (IOException e) {
        e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        
    
    
        
        long endTime = System.currentTimeMillis();
    //    System.out.println(endTime-startTime+"ms");
        System.out.println(i);
        if(max.length()!=0) {
            //System.out.println(max);
            pw.print(max);
            pw.flush();
        }else {
            System.out.println("没有首尾相连");
        }
        
    }
}

  结果截图:

个人总结:

这几次的作业中都有对文件的读取相关的操作,因此也对文件的读写有了一定的了解,就是如果文件里的单词过多如何快速输出结果还没有解决。

原文地址:https://www.cnblogs.com/zhang12345/p/11063836.html