Java实验--关于英文短语词语接龙

mac2022-07-01  3

在课堂上经过实验之后,重新在宿舍里面从0开始编写大概30分钟左右能够完成这个实验,不是原来的思路。

该实验的表述为:从两个文本input1.txt和input2.txt中读取英文单词,若前面的英文单词的尾字母和后面的英文单词的未字母相同的话,则构成一个英文词语接龙,直到文章结尾,求出整篇文章中词语接龙最长的词语接龙词组,并将其输出到output1.txt和output2.txt文件夹中。

实验代码:

package ctn; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; public class Ctn { static List<String> words=new ArrayList<String>(); public static void main(String args[]) throws IOException { words.clear(); if(daoru("input1.txt")) { int size=0; List<String> result = null; while(words.size()>0) { List<String> temp=returnList(words); if(temp.size()>size) { result=temp; size=temp.size(); } } String all=new String(); if(result.size()>1) { for(String it:result) { all+=it+"\r\n"; } daochu(all,"output1.txt"); System.out.println("文件output1.txt导出"); } else System.out.println("单词数量过少无法导出"); } else System.out.println("文件input1.txt不存在"); words.clear(); if(daoru("input2.txt")) { int size=0; List<String> result = null; while(words.size()>0) { List<String> temp=returnList(words); if(temp.size()>size) { result=temp; size=temp.size(); } } String all=new String(); if(result.size()>1) { for(String it:result) { all+=it+"\r\n"; } daochu(all,"output2.txt"); System.out.println("文件output2.txt导出"); } else System.out.println("单词数量过少无法导出"); words.clear(); } else System.out.println("文件input2.txt不存在"); } public static List<String> returnList(List<String> in) { char cold=0; char cnew=0; List<String> temp=new ArrayList<String>(); List<Integer> tempNum=new ArrayList<Integer>(); for(int i=0;i<in.size();i++) { String now=in.get(i); if(i==0) { cold=now.charAt(now.length()-1); tempNum.add(i); temp.add(now); continue; } cnew=now.charAt(0); if(cold==cnew) { tempNum.add(i); if(!temp.contains(now)) { temp.add(now); cold=now.charAt(now.length()-1); } } } for(int j=tempNum.size()-1;j>=0;j--) { in.remove((int)tempNum.get(j)); } return temp; } public static boolean daoru(String path) throws IOException { File a=new File(path); if(!judeFileExists(a)) { System.out.println("文件不存在"); return false; } FileInputStream b = new FileInputStream(a); InputStreamReader c=new InputStreamReader(b,"UTF-8"); { BufferedReader bufr =new BufferedReader(c); String line = null; while((line = bufr.readLine())!=null){ //line是每一行的数据 String ook[]=line.split("[^A-Za-z]"); for(String it:ook) { //it是每一个空格的数据 String temp=it.toLowerCase().replace("\'", "").replace(",", "").replace(".", "").replace(":", "").replace("!", ""); if(temp.length()>0) words.add(temp); } } bufr.close(); } c.close(); b.close(); return true; } //导入文件时判断文件存在 public static boolean judeFileExists(File file) { if (file.exists()) { return true; } else { return false; } } public static void daochu(String txt,String outfile) throws IOException { File fi=new File(outfile); FileOutputStream fop=new FileOutputStream(fi); OutputStreamWriter ops=new OutputStreamWriter(fop,"UTF-8"); ops.append(txt); ops.close(); fop.close(); } }

该实验过程中

对input1.txt输入飘的英文小说的第一章内容,输出output1.txt的时间响应应该在毫秒级以内。(单词量1W左右)

对input2.txt输入飘的整本英文小说的内筒后,输出output2.txt的时间响应应该在5分钟左右。(单词量50W左右)

 

因此上述代码的算法对于数的运算过程响应的时间较长,推断是List中读取N个数据所耗费的时间太长,但是经过了把代码修改成HashMap和Vector对比之后(算法一样的情况下),上面的代码在处理速度已经是最优了

对于Vector来说:处理1w单词就需要耗费数秒,对于50w词的数据就更不用说了

对于Map来说:处理1w单词的时候和List都在1秒以内,50w单词的处理未经过测试

Map的成语接龙实验代码:

package ctn; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Ctn2 { static Map<Integer,String> words=null; public static void main(String args[]) throws IOException { //words.clear(); //System.out.println("导入Input1.txt"); words=new HashMap<Integer,String>(); if(daoru("input1.txt")) { //System.out.println("导入成功"); int size=0; Map<Integer,String> result = null; int maxSize=words.size(); while(words.size()>0) { Map<Integer,String> temp=returnMap(words); if(temp.size()>size) { result=temp; size=temp.size(); } } String all=new String(); if(result.size()>1) { for(int i=0;i<maxSize;i++) { if(result.get(i)!=null) all+=result.get(i)+"\r\n"; } daochu(all,"output1.txt"); System.out.println("文件output1.txt导出"); } else System.out.println("单词数量过少无法导出"); } else System.out.println("文件input1.txt不存在"); System.out.println("开始清空Words"); words.clear(); System.out.println("Words清空完毕"); System.out.println("导入Input2.txt"); if(daoru("input2.txt")) { System.out.println("导入成功"); int size=0; Map<Integer,String> result = null; int maxSize=words.size(); while(words.size()>0) { Map<Integer,String> temp=returnMap(words); if(temp.size()>size) { result=temp; size=temp.size(); } } String all=new String(); if(result.size()>1) { for(int i=0;i<maxSize;i++) { if(result.get(i)!=null) all+=result.get(i)+"\r\n"; } daochu(all,"output2.txt"); System.out.println("文件output2.txt导出"); } else System.out.println("单词数量过少无法导出"); } else System.out.println("文件input2.txt不存在"); } //将元素对应到Map上,如何判断已经检查过? public static Map<Integer,String> returnMap(Map<Integer,String> in) { char cold=0; char cnew=0; Map<Integer,String> temp=new HashMap<Integer,String>(); List<Integer> tempNum=new ArrayList<Integer>(); boolean g_firstStart=false; int g_start=0; for(Integer i:in.keySet()) { if(!g_firstStart) { g_firstStart=true; g_start=i; } String now=in.get(i); if(i==g_start) { cold=now.charAt(now.length()-1); temp.put(i, now); tempNum.add(i); continue; } cnew=now.charAt(0); if(cold==cnew) { temp.put(i, now); tempNum.add(i); cold=now.charAt(now.length()-1); } } for(Integer z:tempNum) { in.remove(z); } return temp; } public static boolean daoru(String path) throws IOException { File a=new File(path); if(!judeFileExists(a)) { System.out.println("文件不存在"); return false; } FileInputStream b = new FileInputStream(a); InputStreamReader c=new InputStreamReader(b,"UTF-8"); { BufferedReader bufr =new BufferedReader(c); String line = null; Integer i=0; while((line = bufr.readLine())!=null){ //line是每一行的数据 String ook[]=line.split("[^A-Za-z]"); for(String it:ook) { //it是每一个空格的数据 String temp=it.toLowerCase().replace("\'", "").replace(",", "").replace(".", "").replace(":", "").replace("!", ""); if(temp.length()>1) { words.put(i,temp); i+=1; } } } bufr.close(); } c.close(); b.close(); return true; } //导入文件时判断文件存在 public static boolean judeFileExists(File file) { if (file.exists()) { return true; } else { return false; } } public static void daochu(String txt,String outfile) throws IOException { File fi=new File(outfile); FileOutputStream fop=new FileOutputStream(fi); OutputStreamWriter ops=new OutputStreamWriter(fop,"UTF-8"); ops.append(txt); ops.close(); fop.close(); } }

 在算法方面我找不到更优的解法,对于老师要求的几百W个单词中词语接龙的算法,还是得进一步探索后才能知道,虽然单词读入的过程中不会死机,但是要想在1分钟内实现百万级别的单词的找出最长的成语接龙还需要很长的1段路需要走。

现在暂时挂起,以后有能力再继续挑战。

转载于:https://www.cnblogs.com/halone/p/10988864.html

最新回复(0)