13-计算最长英语单词链

Posted on 2019-06-09 17:27  咳咳你  阅读(135)  评论(0编辑  收藏  举报

单词接龙,找出最长单词链,例如, 文件里有: Apple Zoo Elephant Under Fox Dog Moon Leaf Tree Pseudopseudohypoparathyroidism 最长的相连英语单词串为: apple - elephant – tree, 输出到文件里面,是这样的: Apple Elephant Tree

统一输入文件名称:input1.txt, input2.txt 统一输出文件名称:output1.txt,output2.txt 程序需要考虑下列异常状况: 例如,文件不存在,你的程序会崩溃么,还是能优雅地退出并给用户提示信息? 如果文件没有任何单词、只有一个单词、没有可以首尾相连的单词,程序应该如何输出? 如果输入文件有一万个单词,你的程序能多快输出结果? 

看到这个题,想了想老师提醒的思路,把每个单词的第一个字母和最后一个字母分别放入数组中,然后第一个数组中的每个数据跟第二个数组中的字母一一进行比较,有相同的就将这两个字母提取,并且在往后比较,再从对应字母确定单词,确定最长单词链。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
public class Lane4 {
 
    public static void main(String[] args) throws IOException {
 
        String filename ="C:\\input.txt";
        File  a=new  File(filename);
     //judeFileExists(a);
     if(judeFileExists(a))
        {
            danci(filename);
        }
     else
        {}
         
         
    }
 
    public static void danci(String s) throws IOException {
            
            BufferedReader br = new BufferedReader(new FileReader(s));
            StringBuffer sb = new StringBuffer();
            String text = null;
            while ((text = br.readLine()) != null) {
                sb.append(text);
            }
            br.close();
            String str = sb.toString().toLowerCase();
            String[] words = str.split("[^(a-zA-Z)]+");
            StringBuffer yao = new StringBuffer();
            String b1=words[0];
            yao.append(b1);
            yao.append(" ");
            //System.out.println(b1);
            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))
            {
                end=words[i].substring(words[i].length()-1,words[i].length());
                yao.append(words[i]);
                yao.append(" ");
            }
           }
            
          // for( String a:words)
          // {
             //  System.out.println(a);
        //   }
         //  System.out.println(yao.toString());
           File file =new File("C:\\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 judeFileExists(File file) {
 
    if (file.exists()) {
        System.out.println("查看输出文件");
        return true;
    } else {
        System.out.println("不存在");
        return false;
    }
}
 
public static void judeDirExists(File file) {
 
    if (file.exists()) {
        if (file.isDirectory()) {
            System.out.println("dir exists");
        } else {
            System.out.println("the same name file exists, can not create dir");
        }
    } else {
        System.out.println("dir not exists, create it ...");
        file.mkdir();
    }
 
}
}