在课堂上经过实验之后,重新在宿舍里面从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. PAT Basic 1077

    1077 互评成绩计算 在浙大的计算机专业课中,经常有互评分组报告这个环节.一个组上台介绍自己的工作,其他组在台下为其表现评分.最后这个组的互评成绩是这样计算的:所有其他组的评分中,去掉一个最高分和一 ...

  2. Centos7 install Openstack Juno (RDO) (转载)

    原文地址:http://www.hdume.com/centos-7-0%E5%AE%89%E8%A3%85openstack/ 1.安装系统,Centos7镜像采用CentOS-7.0-1406-x ...

  3. bash 统计在线时长最长的十个玩/统计一天内一直处于不活跃状态的玩家的百分比

    1.某游戏的客户端每隔5分钟会向服务端报告一次玩家的账户积分,如果两次报告的时间间隔不大于5分钟,认为该玩家在这5分钟内在线,假设报告数据的格式如下: IP                   Dat ...

  4. jenkins 之 iOS 打包及上传至蒲公英

    准备条件 iMAC(要 Mac OS 系统,安卓 和 苹果 可以在同一台电脑上打包) xcode 最新版,要已安装对应的开发证书(生成一个 Ad-Hoc 类型的包就有了) brew(当前管理员账户安装 ...

  5. 03_HibernateSessionFactory源码分析

    文章导读: 讲解了一个线程为什么要使用同一个connection, 我们分析了HiberatenSessionFactory的实现机制, 然后根据Hibernate的写法重构了我们的代码. 最后测试可 ...

  6. STL学习笔记2--list

    List --- 双向列表 List是线性列表结构,数据查找需要一个接一个,不能直接得到元素地址,检索时间与目标元素的位置成正比.但是插入数据比较快,可以在任何位置插入数据或者删除数据.list特点是 ...

  7. webdriver高级应用- 测试过程中发生异常或断言失败时进行屏幕截图

    封装了三个类来实现这个功能: 1.DataUtil.py 用于获取当前的日期以及时间,用于生成保存截图文件的目录名,代码如下: #encoding=utf-8 import time from dat ...

  8. Spring IOC(控制反转)详解及示例

    控制反转——Spring通过一种称作控制反转(IOC)的技术促进了低耦合.当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象.你可以认为IoC与JN ...

  9. PHP的发展史,功能与特点

    web1.0时代:所有的代码都是在浏览器端执行的静态脚本,用户请求的也都是服务器上事先已经存在的静态网页,用户和服务器之间不能进行任何的交互!(不需要数据库的支持) web2.0时代:用户和服务器之间 ...

  10. [CODEVS1916] 负载平衡问题(最小费用最大流)

    传送门 输入所有 a[i],求出平均值 sum,每个 a[i] -= sum 那么如果 a[i] > 0,从 s 向 i 连一条容量为 a[i] 费用为 0 的有向边 如果 a[i] < ...