zoukankan      html  css  js  c++  java
  • 计算最长单词链

    题目要求:

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

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

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

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

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

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

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

    package longword;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    public class Main {
        public static int findwordlong(String text){
            String[] array = {".",",","?","!"};
            for (int i1 = 0; i1 < array.length; i1++) {
                text = text.replace(array[i1]," ");
            }
            String[] textArray = text.split(" ");
            return textArray.length;
        }
        public static String[] findword(String text){
            String[] array = {".",",","?","!","“","”"," "};
            for (int i1 = 0; i1 < array.length; i1++) {
                text = text.replace(array[i1]," ");
            }
            String[] textArray = text.split(" ");
            return textArray;
        }
        public static String readtxt(String txt) throws IOException
        {
            File file = new File(txt);//定义一个file对象,用来初始化FileReader
            FileReader reader = null;
            try {
                reader = new FileReader(file);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//定义一个fileReader对象,用来初始化BufferedReader
            BufferedReader bReader = new BufferedReader(reader);//new一个BufferedReader对象,将文件内容读取到缓存
            StringBuilder sb = new StringBuilder();//定义一个字符串缓存,将字符串存放缓存中
            String s = "";
            while ((s =bReader.readLine()) != null) {//逐行读取文件内容,不读取换行符和末尾的空格
            sb.append(s);//将读取的字符串添加换行符后累加p存放在缓存中
            }
            bReader.close();
            String str = sb.toString();
            return str;
        }
        public static void appendMethodB(String fileName, String content) {
            try {
                //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件,如果为 true,则将字节写入文件末尾处,而不是写入文件开始处 
                FileWriter writer = new FileWriter(fileName, true);
                writer.write(content);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static boolean judeFileExists(String txt) throws IOException {
            File file = new File(txt);
            if (!file.exists()) {
                System.out.println("文件不存在!");
                   return false;
            } 
            else if(file.length() == 0)
            {
                System.out.println("文件为空!");
                return false;
            }
            String str=readtxt("input.txt");
            if(findwordlong(str)==1)
            {
                System.out.println("文件只有一个单词!");
                   return false;
            }
            else
                {
                    return true;
                }
       }
       public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            if(judeFileExists("input.txt"))
            {
                String str=readtxt("input.txt");
                int i;
                int l= findwordlong(str);
                String []word=new String[l];
                char []first=new char[l];
                char []tail=new char[l];
                word=findword(str);
                for(i=0;i<l;i++)
                {
                    if(word[i].length()!=0)
                {
                    first[i]=word[i].charAt(0);
                    tail[i]=word[i].charAt(word[i].length()-1);
                    System.out.println(word[i]);
                }
                }
                String stemp=word[0];
                appendMethodB("output.txt", word[0]);
                for(i=1;i<l;i++)
                {
                    if(first[i]==stemp.charAt(stemp.length()-1))
                    {
                        stemp=stemp+"-"+word[i];
                        appendMethodB("output.txt", "-"+word[i]);
                    }
                }
                if(stemp.equals(word[0]))
                {
                    System.out.println("没有首尾相接的单词!");
                }
                else{
                    System.out.println(stemp);
                }
            }
        }
    }
  • 相关阅读:
    有点自作聪明
    curl 一个无比有用的网站开发工具
    [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
    [Swift]LeetCode888. 公平的糖果交换 | Fair Candy Swap
    [Swift]LeetCode887. 鸡蛋掉落 | Super Egg Drop
    [Swift]LeetCode886. 可能的二分法 | Possible Bipartition
    [Swift]LeetCode885. 螺旋矩阵 III | Spiral Matrix III
    [Swift]LeetCode884. 两句话中的不常见单词 | Uncommon Words from Two Sentences
    [Swift]LeetCode883. 三维形体投影面积 | Projection Area of 3D Shapes
    [Swift]LeetCode882. 细分图中的可到达结点 | Reachable Nodes In Subdivided Graph
  • 原文地址:https://www.cnblogs.com/lishengming00/p/11068149.html
Copyright © 2011-2022 走看看