2023.3.5-课堂练习01题目:计算最长英语单词链

简介: 2023.3.5-课堂练习01题目:计算最长英语单词链

课堂练习01题目:计算最长英语单词链。
一、题目内容:
大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。
最长的定义是:最多单词数量,和单词中字母的数量无关。
二、题目要求:
1、统一输入文件名称:
input1.txt, input2.txt
2、统一输出文件名称:output1.txt,output2.txt
3、程序需要考虑下列异常状况:
(1)例如,文件不存在,你的程序会崩溃么,还是能优雅地退出并给用户提示信息?
(2)如果文件没有任何单词、只有一个单词、没有可以首尾相连的单词,程序应该如何输出?
(3)如果输入文件有一万个单词,你的程序能多快输出结果?

网络异常,图片无法展示
|

网络异常,图片无法展示
|

package jm0227;


import java.io.*;


publicclass p {


   publicstaticvoid main(String[] args) throws IOException {

       String filename = "input3981.txt";

       File a = new File(filename);

       File file = new File("output3981.txt");

       long startTime = System.currentTimeMillis();

       if (judeFileExists(a)) {

           danci(filename);

       } else {

           try {


               FileWriter fw = new FileWriter(file);

               fw.write("无此文件");

               fw.flush();

               fw.close();

           } catch (IOException e) {

               e.printStackTrace();

           }

       }

       long endTime = System.currentTimeMillis();

       System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间

   }


   publicstaticvoid 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(" ");

       if (b1.equals("")) {

           System.out.println("文件为空");

           yao.append("文件为空");

       } else {

           if (words.length == 1) {

               System.out.println("只有一个单词");

           }


           String end = b1.substring(b1.length() - 1, b1.length());

           for (int i = 1; i < words.length; i++) {


               String start = words[i].substring(0, 1);

               if (end.equals(start)) {

                   if (judechong(yao, words[i])) {

                   } else {

                       end = words[i].substring(words[i].length() - 1, words[i].length());

                       yao.append(words[i]);

                       yao.append(" ");

                   }


               }

           }


           if (yao.toString().equals(words[0] + " ")) {

               yao.append("无互联语句");

               System.out.println("无互联词");

           }

       }


       File file = new File("output3981.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();

       }


   }


   publicstaticboolean judechong(StringBuffer yao, String word) {

       String a = yao.toString();

       boolean flag = false;

       String[] words = a.split("[^(a-zA-Z)]+");

       for (int i = 0; i < words.length; i++) {

           if (word.equals(words[i])) {

               flag = true;

           }

       }

       return flag;

   }


   publicstaticboolean judeFileExists(File file) {


       if (file.exists()) {

           System.out.println("文件存在");

           returntrue;

       } else {

           System.out.println("文件不存在");

           returnfalse;

       }

   }


}

网络异常,图片无法展示
|

先把单词读取,再进行逐个判断,最后输出结果。

相关文章
|
3月前
leetcode-521:最长特殊序列 Ⅰ
leetcode-521:最长特殊序列 Ⅰ
35 0
|
10月前
|
算法
代码随想录Day21 回溯 LeetCodeT216 组合总和III LeetCode T17电话号码的字母总和
代码随想录Day21 回溯 LeetCodeT216 组合总和III LeetCode T17电话号码的字母总和
42 0
|
2月前
|
存储 算法 测试技术
力扣经典150题第三十三题:最小覆盖子串
力扣经典150题第三十三题:最小覆盖子串
14 1
|
1月前
|
存储 算法 索引
1124. 表现良好的最长时间段 (python) 前缀和 分类讨论 最大长度 力扣 面试题
1124. 表现良好的最长时间段 (python) 前缀和 分类讨论 最大长度 力扣 面试题
|
2月前
|
存储 算法
力扣经典150题第四十六题:最长连续序列
力扣经典150题第四十六题:最长连续序列
12 0
|
2月前
|
存储 算法 程序员
力扣经典150题第三十一题:无重复字符的最长子串
力扣经典150题第三十一题:无重复字符的最长子串
18 0
|
2月前
|
算法
【经典LeetCode算法题目专栏分类】【第8期】滑动窗口:最小覆盖子串、字符串排列、找所有字母异位词、 最长无重复子串
【经典LeetCode算法题目专栏分类】【第8期】滑动窗口:最小覆盖子串、字符串排列、找所有字母异位词、 最长无重复子串
|
3月前
|
算法 Java C++
数据结构与算法面试题:给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。(提示:使用动态规划或者中心扩散)
数据结构与算法面试题:给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。(提示:使用动态规划或者中心扩散)
66 0
|
9月前
|
算法
代码随想录算法训练营第二十六天 | LeetCode 39. 组合总和、40. 组合总和 II、131. 分割回文串
代码随想录算法训练营第二十六天 | LeetCode 39. 组合总和、40. 组合总和 II、131. 分割回文串
36 0