在课堂上经过实验之后,重新在宿舍里面从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. POJ:2060-Taxi Cab Scheme(最小路径覆盖)

    传送门:http://poj.org/problem?id=2060 Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Sub ...

  2. HDU 4812 D Tree 树分治

    题意: 给出一棵树,每个节点上有个权值.要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\) ...

  3. Python使用asyncio+aiohttp异步爬取猫眼电影专业版

    asyncio是从pytohn3.4开始添加到标准库中的一个强大的异步并发库,可以很好地解决python中高并发的问题,入门学习可以参考官方文档 并发访问能极大的提高爬虫的性能,但是requests访 ...

  4. net clr via c sharp chap1-- note

    Tag-> 托管代码 Tag-> .NET Framework 系统环境检测 Tag-> 设置平台 Tag-> 查询64或32位机 Tag-> IL编译成机器指令 Tag ...

  5. MongoDB学习-->设置通用的自增ID替代ObjectId

    插入mongodb数据时,会为其分配一个随机id,想要设置通用的自增id,可以进行以下操作 1.创建自增序列 package com.tangzhe.autoid; import lombok.Dat ...

  6. 查找最小的k个元素 【微软面试100题 第五题】

    题目要求: 输入n个整数,输出其中最小的k个. 例如:输入1,2,3,4,5,6,7,8这8个数字,则最小的4个数字为1,2,3,4. 参考资料:剑指offer第30题. 题目分析: 解法一: 用快排 ...

  7. Selenium WebDriver-actionchain模拟键盘操作

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver from selen ...

  8. tomcat(不仅仅是tomcat)通过熵池解决在linux启动应用慢

    tomcat启动过程中报错 -Jul- ::] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web applica ...

  9. BZOJ3993 [SDOI2015]星际战争 【二分 + 网络流】

    题目 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai.当一个巨型机器人的装甲值减少到 ...

  10. 【bzoj1191】[HNOI2006]超级英雄Hero - 二分图匹配

    现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回答一道题后,才能进入下一 ...