Java实验--关于英文短语词语接龙
在课堂上经过实验之后,重新在宿舍里面从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实验--关于英文短语词语接龙的更多相关文章
- 第三次Java实验报告
Java实验报告 班级 计科二班 学号20188437 姓名 何磊 完成时间 2019/9/22 评分等级 实验三 String类的应用 实验目的 掌握类String类的使用: 学会使用JDK帮助文档 ...
- JAVA实验三及总结
JAVA第五周作业 Java实验报告三 第一题 1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) (1).统计该字符 ...
- Java实验五
20145113 Java实验五 网络编程及安全 实验内容 对于客户端与服务器端:修改原代码,使其可以实现连续的传消息,并且传送文件. 对于加解密部分: 对于原先的加密只加密"hello w ...
- Java实验四
20145113 Java实验四 快捷键 之前没怎么记ISDEA的快捷键,但是熟练使用快捷键可以带来很多的便利,于是先开始学习一些常用的快捷键,就采用它默认的快捷键,这样后期就不会出现冲突,一些and ...
- Java实验一
20145113 Java实验一 使用JDK编译.运行简单的Java程序 安装JDK并配置相关环境 安装JDK后配置环境变量 计算机→属性→高级系统设置→高级→环境变量 新建 JAVA_HOME 变量 ...
- Java实验二20135104
课程:Java程序设计 班级: 1351 姓名:刘帅 学号:20135104 成绩: 指导教师:娄嘉鹏 实验日期:2 ...
- Java实验报告五:Java网络编程及安全
Java实验报告五:Java网络编程及安全 ...
- Java实验报告二:Java面向对象程序设计
Java实验报告二:Java面向对象程序设计 ...
- java实验一实验报告
Java实验报告一:Java开发环境的熟悉 ...
随机推荐
- linux用户和用户组管理详解
Linux 用户和用户组管理 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. 用户的账号一方面可以帮助 ...
- python基础学习笔记——面向对象初识
面向对象初识 python中一切皆对象. 类有两种: 新式类:在py3中所有类都是新式类 经典类:在py2中只有类本身继承了object类才叫做新式类,默认是经典类 class Person: cou ...
- JAVA 基础--开发环境IDEA 搭建
1.下载IDEA (500M+) 2.激活. 在网站http://idea.lanyus.com/中获取注册码,填入Activation code中: 然后点击Activate即可. 3.创建工程前 ...
- monkey测试工具与常用的linux命令
Monkey测试工具 说明:monkey是一个安卓自带的命令行工具,可以模拟用户向应用发起一定的伪随机事件.主要用于对app进行稳定性测试与压力测试. 实现:首先需要安装一个ADB工具,安装完之后,需 ...
- jquery源码区块
- 使用MySQLWorkBench连接数据库
在日常使用中,经常使用命令行去连接MySQL数据库不方便,推荐使用MySQLWorkBench去远程连接进行管理,下面记录一下使用步骤: 1. 安装MySQLWorkBench后,界面如下,点击“+” ...
- [笔记]Docker解决了什么问题?
Docker的优势: 环境依赖问题 更轻量的虚拟化,节省了虚拟机的性能损耗 Docker应用场景: 程序分发,gitlab的安装很恶心吧,所以有人做了gitlab的image 部署发布,这点对运维的同 ...
- python多进程(multiprocessing)
最近有个小课题,需要用到双进程,翻了些资料,还算圆满完成任务.记录一下~ 1.简单地双进程启动 同时的调用print1()和print2()两个打印函数,代码如下: #/usr/bin/python ...
- 关于 __int128
__int128 是 GCC 提供的扩展(extension),可以当作 128 位整数使用. 关于 __int128 和 __int128_t Normally, _t suffix means a ...
- HDU-3592 World Exhibition
差分约束. 很容易看出两种约束方式,然后建图.而且题目要求排序不能乱,于是加上第三种约束. 求最长就跑一遍最短路啊就行了. #include <cstdlib> #include < ...