计算最长英语单词链

要求:

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

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

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

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

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

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

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

设计思想:

可以通过函数把每个单词的首尾字母存到数组里,然后就是字母进行比较配对啦。

  1 package analyse_word;
  2 import java.io.File;
  3 import java.io.FileNotFoundException;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.PrintStream;
  7 import java.util.Scanner;
  8 public class letter_follow_each_other_best {
  9  
 10  public static void OutputStream(String max) {
 11 //  将数据写入文件
 12         PrintStream ps = null;
 13         try {
 14             ps = new PrintStream(new FileOutputStream("D:\empty.txt"));
 15             //将标准输出重定向到ps输出流
 16             System.setOut(ps);
 17             //向标准输出输出一个字符串
 18             System.out.println(max);
 19             //向标准输出输出一个对象
 20         } catch (IOException ex) {
 21             ex.printStackTrace();
 22         } finally {
 23             if (ps != null) {
 24                 ps.close();
 25             }
 26         }
 27  }
 28  public static void main(String[] args) throws FileNotFoundException {
 29   File file = new File("D:\letter_follow_each_other_best.txt");// 读取文件
 30   if (!file.exists()) {// 如果文件打不开或不存在则提示错误
 31    System.out.println("文件不存在");
 32    return;
 33   }else {
 34    if(file.exists() && file.length() == 0) {  
 35        System.out.println("文件为空!");  
 36        return;
 37    }  
 38   }
 39   long startTime = System.currentTimeMillis();
 40   String[] strs=new String[1000000];
 41   Scanner x = new Scanner(file);
 42   int i=0;
 43   boolean flag=false;
 44   while(x.hasNextLine()) {
 45    String[] str=x.nextLine().split("\W+");
 46    for(int ms=0;ms<str.length;ms++) {
 47     if(!str[ms].equals("")&&str[ms].length()>2) {
 48      flag=false;
 49 //     System.out.println(str[ms]);
 50      if(i!=0) {
 51       for(int t=0;t<i;t++) {
 52        if(!str[ms].equals(strs[t])) {
 53         flag=true;
 54        }
 55       }
 56      }else {
 57       flag=true;
 58      }
 59      
 60      if(flag) {
 61       strs[i]=str[ms];
 62       i++;
 63      }
 64      
 65     }
 66     
 67    }
 68   }
 69   if(i==1) {
 70    System.out.println("该文件只有一个单词!无法实现词语接龙");
 71   }
 72   String sentence = "";
 73   String word="";
 74   String max="";
 75   for(int m=0;m<i;m++) {
 76    sentence = strs[m];
 77    word = sentence;
 78    for(int j=m+1;j<i;j++) {
 79     if(strs[j].toLowerCase().subSequence(0, 1).equals(word.toLowerCase().subSequence(word.length()-1, word.length()))) {
 80      word = strs[j];
 81      sentence+="-"+word;
 82     }
 83    }
 84    
 85    if(sentence.indexOf("-")!=-1) {
 86     if(sentence.length()>max.length()) {
 87      max = sentence;
 88     }
 89    }
 90    
 91   }
 92   long endTime = System.currentTimeMillis();
 93   System.out.println(endTime-startTime+"ms");
 94   System.out.println(i);
 95   if(max.length()!=0) {
 96    OutputStream(max);
 97   }else {
 98    System.out.println("没有首尾相连");
 99   }
100   
101  }
102 }
原文地址:https://www.cnblogs.com/mawangwang/p/11071612.html