在课堂上经过实验之后,重新在宿舍里面从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段路需要走。

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

Java实验--关于英文短语词语接龙的更多相关文章

  1. 第三次Java实验报告

    Java实验报告 班级 计科二班 学号20188437 姓名 何磊 完成时间 2019/9/22 评分等级 实验三 String类的应用 实验目的 掌握类String类的使用: 学会使用JDK帮助文档 ...

  2. JAVA实验三及总结

    JAVA第五周作业 Java实验报告三 第一题 1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) (1).统计该字符 ...

  3. Java实验五

    20145113 Java实验五 网络编程及安全 实验内容 对于客户端与服务器端:修改原代码,使其可以实现连续的传消息,并且传送文件. 对于加解密部分: 对于原先的加密只加密"hello w ...

  4. Java实验四

    20145113 Java实验四 快捷键 之前没怎么记ISDEA的快捷键,但是熟练使用快捷键可以带来很多的便利,于是先开始学习一些常用的快捷键,就采用它默认的快捷键,这样后期就不会出现冲突,一些and ...

  5. Java实验一

    20145113 Java实验一 使用JDK编译.运行简单的Java程序 安装JDK并配置相关环境 安装JDK后配置环境变量 计算机→属性→高级系统设置→高级→环境变量 新建 JAVA_HOME 变量 ...

  6. Java实验二20135104

    课程:Java程序设计          班级: 1351 姓名:刘帅                学号:20135104 成绩:             指导教师:娄嘉鹏       实验日期:2 ...

  7. Java实验报告五:Java网络编程及安全

    Java实验报告五:Java网络编程及安全                                                                               ...

  8. Java实验报告二:Java面向对象程序设计

    Java实验报告二:Java面向对象程序设计                                                                               ...

  9. java实验一实验报告

    Java实验报告一:Java开发环境的熟悉                                                                               ...

随机推荐

  1. apply 与 lambda

    Python中的lambda和apply用法  https://blog.csdn.net/anshuai_aw1/article/details/82347016

  2. hdu3487Play with Chain

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. String、StringBuffer和StringBuilder,定义一个自己的StringBuilder的类

    String Java中的字符串值属于String类,虽然有其它方法表示字符串(如字符数组),但Java一般使用String类作为字符串的标准格式,Java编译器把字符串值作为String对象; St ...

  4. 九度oj 1003

    前几天开刷九度oj,准备把做的题都放上,先放1003 题目1003:A+B             时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:16923 解决:7029 题目描述: 给 ...

  5. 算法复习——树形dp

    树形dp的状态转移分为两种,一种为从子节点到父节点,一种为父节点到子节点,下面主要讨论子节点到父亲节点的情况: 例题1(战略游戏): 这是一道典型的由子节点状态转移到父节点的问题,而且兄弟节点之间没有 ...

  6. [解决方案]Window 2008 R2 + IIS7.5 + VS2013 错误代码 0x80070002

    HTTP 错误 404.0 - Not Found 您要找的资源已被删除.已更名或暂时不可用.详细错误信息模块 IIS Web Core通知 MapRequest Handler处理程序 Static ...

  7. vim 翻页命令记录

    vim命令: ctrl-f:往前翻一页(forward) ctrl-b:往后翻一页(backward) ​ ctrl-d:往下翻半页(down) ctrl-u:往上翻半页(up) ​​​

  8. element-ui复合型输入框的显示问题

    按照官方文档那样发现,最左边的选择器里面的字显示不出来,解决方法是在为el-select设置宽度,就可以正常显示了

  9. 2015-2016 ACM-ICPC Northeastern European Regional Contest (NEERC 15)

    NEERC 15 题解1 题解2 官方题解

  10. FZOJ Problem 2219 StarCraft

                                                                                                        ...