zoukankan      html  css  js  c++  java
  • 单词接龙最长——6.6——计算最长英语单词链

    题目要求:

    大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。最长的定义是:最多单词数量,和单词中字母的数量无关。
    例如, 文件里有:
    Apple
    Zoo
    Elephant
    Under
    Fox
    Dog
    Moon
    Leaf
    Tree
    Pseudopseudohypoparathyroidism
    最长的相连英语单词串为:  apple - elephant – tree,  输出到文件里面,是这样的:
                  Apple
                  Elephant
                  Tree
    要求程序 (WordList.exe)能处理命令行参数,知道什么是输入文件, 输出文件应该放在哪里。  这样,当助教拿到学生的源程序后,就能编译,并运行一系列的测试。
    Wordlist.exe  /i input1.txt   /o  output1.txt
    Wordlist.exe  /i input2.txt   /o  output2.txt
                 …
    既然是测试,就会有很多边角情况,例如,文件不存在,你的程序会崩溃么,还是能优雅地退出并给用户提示信息?如果文件没有任何单词、只有一个单词、没有可以首尾相连的单词,程序应该如何输出?
    程序的正确性验证完毕后, 就可以用一些命令行计时工具来测试程序的效率。 当然,同学们也可以参考《构建之法》的介绍,自己先测试并改进程序的效率, 最好是先写一个朴素的算法,看看用时如何,再分析效率,改进,分析...
    如果输入文件有一万个单词,你的程序能多快输出结果?课程助教会在同一个电脑上用一个大文件来测试所有同学的作业,请做好准备。

    自我想法:

    1.读取文件

    2.读取第一个单词

    3.读取第一个单词的最后一个字母b

    4.读取下一个首字母为刚才最后一个字母的单词并记录其为第n个

    5.循环至没有重复

    6.从第一个单词到第n个单词循环2-5的步骤

    以下是我从各种途径收集整合的代码,如有相似的,就当是我抄的他的

    package text6_6;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Onebyone {
    
    	public static void main(String[] args) throws IOException 
    	{
    		// TODO 自动生成的方法存根
    		String filename ="input1.txt";
    		File  a=new  File(filename);
    		File  file=new  File("output1.txt");
    		long startTime = System.currentTimeMillis();
    	 //judeFileExists(a);
    	 if(judeFileExists(a))
    		{
    			danci(filename);
    		}
    	 else
    	    { 
    		  try {
    	        	
            	  FileWriter fw =new FileWriter(file);
            	  fw.write("文件不存在");
            	  fw.flush();
            	  fw.close();
            }
            catch (IOException e) {
    	           e.printStackTrace();      
    	       }
    	    }
    	 long endTime = System.currentTimeMillis();                                         //获取结束时间
    	}
    
    	public static void danci(String s) throws IOException {
    		   
    			BufferedReader br = new BufferedReader(new FileReader(s));
    			StringBuffer sb = new StringBuffer();
    			String text = null;
    			while ((text = br.readLine()) != null) {
    				sb.append(text);                                          // 将读取出的字符追加到stringbuffer中
    			}
    			br.close();                                                 // 关闭读入流
    			String str = sb.toString().toLowerCase();                               // 将stringBuffer转为字符并转换为小写
    			String[] words = str.split("[^(a-zA-Z)]+");                              // 非单词的字符来分割,得到所有单词
    			StringBuffer yao = new StringBuffer();
    			String b1=words[0];
    			yao.append(b1);
    			yao.append(" ");
    			if(b1.equals(""))
    			{
    				System.out.println("文件为空");
    				yao.append("文件为空");
    			}
    			else
    			{
    				if(words.length==1)
    				{
    					System.out.println("该文件只有一个单词");
    				}
    			   
    			String end=b1.substring(b1.length()-1,b1.length());
    		                                                           // System.out.println(end);
    		   for(int i=1;i<words.length;i++)
    		   {  
    			 
    		    String start=words[i].substring(0,1);
    		    if(end.equals(start))
    		     {
    		    	if(judechong(yao,words[i]))
    		    	{}
    		    	else
    		    	{
    		    	end=words[i].substring(words[i].length()-1,words[i].length());
    		    	yao.append(words[i]);
    		    	yao.append(" ");
    		    	}
    		    	
    		     }
    		    } 
    		    
    		   if(yao.toString().equals(words[0]+" "))
    		    {
    				yao.append("无互联语句");
    				System.out.println("无互联词");
    		    }
    		}
    		
    		   File file =new File("output2.txt");
    	        try {
    	             file.createNewFile();
    	        } catch (IOException e) {
    	           e.printStackTrace();      
    	       }
    	      
    	        try {
    	        	
    	        	  FileWriter fw =new FileWriter(file);
    	        	  fw.write(yao.toString());
    	        	  fw.flush();
    	        	  fw.close();
    	        }
    	        catch (IOException e) {
    		           e.printStackTrace();      
    		       }   
    	}
    public  static boolean  judechong(StringBuffer yao,String word)
    {
    	String a=yao.toString();
    	boolean flag=false;
    	String[] words = a.split("[^(a-zA-Z)]+");                                             // 非单词的字符来分割,得到所有单词
    	for(int i=0;i<words.length;i++)
    	{
    		if(word.equals(words[i]))
    		{
    			flag=true;
    		}
    	}
    	return flag;
    }
                                                                            // 判断文件是否存在
    public static boolean judeFileExists(File file) {
    
        if (file.exists()) {
            System.out.println("文件存在");
            return true;
        } else {
            System.out.println("文件不存在");
            return false;
        }
    }
                                                                              // 判断文件夹是否存在(未修改)
    public static void judeDirExists(File file) {
    
        if (file.exists()) {
            if (file.isDirectory()) {
                System.out.println("dir exists");
            } else {
                System.out.println("the same name file exists, can not create dir");
            }
        } else {
            System.out.println("dir not exists, create it ...");
            file.mkdir();
        }
    
    }
    }
    

      这个代码需要我长时间的复写,仔细思索,加油。

  • 相关阅读:
    99年人民币冠号大全
    9910的5种水印冠号发行量分布图
    儿童、青少年长身高的注意事项
    钱币评级流程与方法
    99版人民币收藏价值分析
    四、五版币冠字号发行量统计排行
    如何收藏第四套人民币大全套
    第四套荧光钞大全介绍
    小程序加载中提示实现
    微信小程序navigateBack如何带参数
  • 原文地址:https://www.cnblogs.com/kmxbf2292/p/10987919.html
Copyright © 2011-2022 走看看