首页 > 其他分享 >课堂练习01:计算最长英语链

课堂练习01:计算最长英语链

时间:2023-02-27 23:58:44浏览次数:35  
标签:01 课堂练习 strs str1 tm str new 英语 String

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

、题目内容:

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

最长的定义是:最多单词数量,和单词中字母的数量无关。

、题目要求:

1、统一输入文件名称:input1.txt, input2.txt

2、统一输出文件名称:output1.txt,output2.txt

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

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

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

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

 

 

 

bug多多,只能实现恰好实现我自己的文件。而且,我直接从第一个单词开始找这一条单词接龙链,并没有全部遍历,也并不是最长的。

 

import java.io.*;
import java.util.Map;
import java.util.TreeMap;

public class class_test {
    public static void main(String[] args) throws IOException {

//        //写入操作
//        try{
//            BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\input1.txt"));
//            String b = null;
//            FileWriter fileWriter = new FileWriter("D:\\\output1.txt",true);
//            while(( b = bufferedReader.readLine()) != null){
//                System.out.println(b);
//                fileWriter.write(b+'\n');
//            }
//            fileWriter.close();
//            bufferedReader.close();
//        }catch (IOException exception){
//            System.out.println(exception);
//        }


        File file = new File("D:\\input1.txt");
        FileWriter fileWriter = new FileWriter("D:\\output1.txt",true);
        try {
            FileReader fr = new FileReader(file);
            BufferedReader bufr = new BufferedReader(fr);
            //按行读取返回的字符串
            String s = null;
            //将所有s字符串拼接
            String all = "";
            while ((s = bufr.readLine()) != null) {
                all = all + s;
            }
            String[] strs = all.split("[^a-zA-Z0-9]");
            int[] n = new int[26];
            for(int i=0;i<26;i++) n[i]=0;
            String[][] r1 = new String[26][strs.length];//存数组按照开头存
//            String[][] r2 = new String[strs.length][strs.length];//按照尾存
            Map<String, Integer> tm = new TreeMap<>();//
            for (int i = 0; i < strs.length; i++) {
                if (!tm.containsKey(strs[i])) {//用来判断map中是否有key值,是否出现过。
                    tm.put(strs[i], 0);//key,value:单词,readed
//                    String start,end;
//                    start = strs[i].substring(0,1);
//                    end = strs[i].substring(strs[i].length()-1,strs[i].length());
                    char[] chs = strs[i].toCharArray();
                    r1[chs[0] - 'a'][n[chs[0] - 'a']++] = strs[i];
//                    r2[chs[strs[i].length() - 1] - 'a'][n[chs[strs[i].length() - 1] - 'a']] = strs[i];
                }
            }

            String rs = "";
            String str = strs[0];
            rs+=str;
            tm.put(str,1);
            while(true) {
                int i=0;
                char[] c = str.toCharArray();
                char ch = c[str.length() - 1];
                for ( i = 0; i < n[ch - 'a']; i++) {
                    String str1 = r1[ch - 'a'][i];
                    if (tm.get(str1) == 0) {
                        tm.put(str1, 1);
                        rs += " " + str1;
                        str = str1;
                        break;
                    } else {
                        continue;
                    }
                }
                if(i>=n[ch-'a'])break;
            }
//            while(tm.get(str) == 0){
//                rs += str;
//                tm.put(str,1);
//                char[] c = str.toCharArray();
//                char ch = c[str.length() - 1];
//                for (int i = 0; i < n[ch - 'a']; i++) {
//                    String str1 = r1[ch - 'a'][i];
//                    if (tm.get(str1) == 0) {
//                        tm.put(str1,1);
//                        rs += " "+ str1;
//                        str=str1;
//                        break;
//                    } else {
//                        continue;
//                    }
//                }
//            }
            System.out.println("最长单词链为:"+rs);
            fileWriter.write(rs+'\n');
            fileWriter.close();
            bufr.close();
            fr.close();
        } catch(Exception e){
        e.printStackTrace();
    }

    }


}

 

input文件:hava apple ah experienc gread dhahaa my yl piua cctv good vpg dog 

output文件:hava apple experienc cctv vpg gread dhahaa ah

控制台输出:hava apple experienc cctv vpg gread dhahaa ah

 

标签:01,课堂练习,strs,str1,tm,str,new,英语,String
From: https://www.cnblogs.com/hmy22466/p/17162400.html

相关文章

  • 计算最长英语单词
    计算最长英语单词链的题目为:大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N个不同的英语单词,我们能否写一个程序,快速找出最长的能首尾相连的英语单词......
  • 【题解】P3747 [六省联考 2017] 相逢是问候
    思维难度作为一道省选题还是有待商榷,但是代码确实挺恶心的。记一下这种有关无穷层幂嵌套(无穷幂塔)的套路。思路扩展欧拉定理+线段树。首先看到不断嵌套幂并且模数较大......
  • Microsoft Office 2016 专业增强版 for Windows 官网下载地址
    MicrosoftOffice2016专业增强版是Microsoft公司推出的一款高级的办公软件套件,包括Word、Excel、PowerPoint、Outlook、OneNote、Access、Publisher、SkypeforBusiness......
  • 今日课上测试题总结-计算最长英语单词链
    今天软工课上老师留的作业总结一下1importjava.io.*;2importjava.util.*;34publicclassMaxlist{56publicstaticvoidmain(String[]args)th......
  • 英语链的接龙
    接龙规则:大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N个不同的英语单词,我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能......
  • 计算最长英语单词链思路
    首先就是做好输入文件读取文件和输出文件,可以在菜鸟教程去找,然后学习代码模板,把读入和读出写好。然后就是解决文件中的换行读取。有的读入写法不能读下行的字符。可以用菜......
  • oracle报错:ORA-01839: date not valid for month specified(指定月份的日期无效)
    场景日期值存的是10位字符串,如2020-02-01,sql筛选时需要选1年以内的。select*fromt_userwhereto_date(app_date,'yyyy-MM-dd')>sysdate-360查看日志发现报错:ORA-01839......
  • 课堂练习01题目:计算最长英语单词链。
     一、题目内容:大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N个不同的英语单词,我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最......
  • pat乙级1014 福尔摩斯的约会
    #include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>intmin(inta,intb);intmin(inta,intb){returna<b?a:b;}intmain(){......
  • 01 列表类型
    #1、作用:按位置存放多个值#2、定义#l=[1,1.2,'a']#l=list([1,1.2,'a'])#print(type(l))#3、类型转换:但凡能够被for循环遍历的类型都可以当做参数传给list()转成列......