Loading


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

例如, 文件里有:

 

Apple

Zoo

Elephant

Under

Fox

Dog

Moon

Leaf

Tree

Pseudopseudohypoparathyroidism

 

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

              Apple

              Elephant

              Tree

题目转自:https://www.cnblogs.com/xinz/p/7119695.html

 

设计思路:读取文件,将文件中的单词写入数组中,遍历数组,获取第一个单词的首尾字母,与其他单词一次比较,如果尾字母与某个字母的首字母相同则尾字母重新赋值,如果不同则比对下一个单词,以此类推。

 

源代码:

package com.yun15;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Dancilian {

    public static void main(String[] args) throws IOException {
        // TODO 自动生成的方法存根
        String filename ="D:\\工作台\\工作台F\\ketang00\\input.txt";
        File  add=new  File(filename);
        if(judeFileExists(a))
        {dancilian(filename);}
        
        
     else
        {
         System.out.println("文件不存在");
        }
         
      }
    
    
    
    //判断文件是否存在
    public static void judeDirExists(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                System.out.println("存在");
            } else {
                System.out.println("存在同名文件,无法创建");
            }
        } else {
            System.out.println("文件不存在创建它");
            file.mkdir();
        }

    }

    
    
    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)]+");
            if(words.length==1)
            {
                if("".equals(words[0]))
                System.out.println("文件中无单词");
                else
                    System.out.println("文件中只有一个单词");
            }

            else
            {
                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(" ");
                }
                
               }

              String t=yao.toString();            
              if("apple ".equals(t))
              {
                  System.out.println("没有首尾相连的单词");
              }
               File file =new File("D:D:\\工作台\\工作台F\\ketang00\\output1.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();      
                   }
            }
         
    }
}

 

 

程序截图:

 

posted on 2019-06-09 20:59  一氓  阅读(156)  评论(0编辑  收藏  举报