• 单词接龙


    一、要求

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

    例如, 文件里有:

    Apple

    Zoo

    Elephant

    Under

    Fox

    Dog

    Moon

    Leaf

    Tree

    Pseudopseudohypoparathyroidism

    最长的相连英语单词串为:  apple - elephant – tree,  输出到文件里面,是这样的:

    Apple

    Elephant

    Tree

    二、分析

      遍历查找,每次找到一个链后,删去最后链里最后一个单词,重新向后查找,这样可以找出所有链,然后再找最长链。

    三、txt文件

    apple earth zoo element text turn monkey rabbit hand table tag good hour

    四、源代码

      1 import java.io.BufferedReader;
      2 import java.io.BufferedWriter;
      3 import java.io.File;
      4 import java.io.FileReader;
      5 import java.io.FileWriter;
      6 import java.io.IOException;
      7 import java.util.ArrayList;
      8 
      9 public class FindLongestChain {
     10 
     11     public static void main(String[] args) throws IOException {
     12         String inputfilename ="F:\Study\Code\Java\单词接龙\input1.txt";
     13         String outputfilename ="F:\Study\Code\Java\单词接龙\output.txt";
     14         File input = judgeFile(inputfilename);
     15         File output = judgeFile(outputfilename);
     16         if(input != null) {
     17             ArrayList<String> list = findLongChain(readFile(input));
     18             if(output != null)
     19                 output(list,output);
     20         }else {
     21             System.out.println("程序无法运行,请检查文件!");
     22         }
     23       }
     24     
     25     
     26     private static File judgeFile(String s) {
     27         try {
     28             File file = new File(s);
     29             if (file.exists()) {
     30                 if (file.isDirectory()) {
     31                     System.out.println("当前路径存在同名文件夹!");
     32                     return null;
     33                 } else {
     34                     return file;
     35                 }
     36             } else {
     37                 System.out.println("文件" + file + "不存在,正在创建该文件!");
     38                 if(!file.getParentFile().exists()) {
     39                     if(file.getParentFile().mkdirs()) {
     40                         System.out.println("创建文件夹" + file.getParentFile() + "成功!");
     41                     } else {
     42                         System.out.println("创建文件夹" + file.getParentFile() + "失败!");
     43                         return null;
     44                     }
     45                 }
     46                 if(file.createNewFile()) {
     47                     System.out.println("创建文件 " + file + " 成功!");
     48                     return file;
     49                 }else {
     50                     System.out.println("创建文件"+ file +"失败!");
     51                     return null;
     52                 }
     53             }
     54         } catch (IOException e) {
     55             e.printStackTrace();
     56         }
     57         return null;
     58     }
     59 
     60     private static ArrayList<String> readFile(File file){
     61         ArrayList<String> list = new ArrayList<String>();
     62         try {
     63             BufferedReader br = new BufferedReader(new FileReader(file));
     64             int c;
     65             String s = "";
     66             while((c = br.read()) != -1) {
     67                 if(c<65 || (c>90&&c<96) || c>122) {
     68                     if(s != "" && !list.contains(s))
     69                         list.add(s);
     70                     s = "";
     71                 } else {
     72                     s += (char)c;
     73                     s.toLowerCase();   //转成小写,便于比较
     74                 }
     75             }
     76             if(s != "" && !list.contains(s))  //如果最后读取到的不是分隔符就说明并未将s存进list中
     77                 list.add(s);
     78             br.close();
     79         }catch(Exception e) {
     80             e.printStackTrace();
     81         }
     82         return list;
     83     }
     84     
     85     private static ArrayList<String> findLongChain(ArrayList<String> l) {
     86         ArrayList<ArrayList<String>> chains = new ArrayList<ArrayList<String>>();
     87         ArrayList<String> list;
     88         do {
     89             list = new ArrayList<String>();
     90             list.add(l.get(0));
     91             int location = 0;  //字符串列表当前查到的索引位置
     92             String head,tail;  //单词首字母和尾字母
     93             while(location < l.size()) {
     94                 tail = list.get(list.size()-1).substring(list.get(list.size()-1).length()-1,list.get(list.size()-1).length());
     95                 head = l.get(location).substring(0,1);
     96                 if(head.compareTo(tail) == 0)
     97                     list.add(l.get(location));
     98                 location++;
     99             }
    100             chains.add(list);
    101             for(int i=0;i<l.size();i++)          //找到单词链的最后一个单词,下次遍历跳过该词
    102                 if(l.get(i) == list.get(list.size()-1))
    103                     l.set(i,"/" + l.get(i));
    104         }while(list.size() != 1);     //当单词链只有一个单词时,停止查找
    105         int max = 0;
    106         for(ArrayList<String> c:chains) {    //遍历查找单词链列表中最长单词链
    107             if(max < c.size()) {
    108                 max = c.size();
    109                 list = c;
    110             }
    111             /*for(String str:c)
    112                 System.out.println(str);
    113             System.out.println();*/
    114         }
    115         /*for(String str:list)
    116             System.out.println(str);*/
    117         return list;
    118     }
    119     
    120     private static void output(ArrayList<String> list, File file) {
    121         try {
    122             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
    123             for(String str:list) {
    124                 bw.write(str);
    125                 bw.newLine();
    126             }
    127             bw.close();
    128         } catch (IOException e) {
    129             e.printStackTrace();
    130         }
    131     }
    132 }
  • 相关阅读:
    2019年11月4日随堂测试 最多输入字母统计
    写增删改查中间遇到的问题
    2019年12月9日下午自习成果
    2019年12月16日 分级考试
    2019年11月18日 JAVA期中考试 增删改查
    sql语言积累
    【转载】Java项目中常用的异常处理情况总结
    泛型
    C#数字格式化输出
    委托,Lambda的几种用法
  • 原文地址:https://www.cnblogs.com/dream0-0/p/11069074.html
Copyright © 2020-2023  润新知