zoukankan      html  css  js  c++  java
  • 成语接龙(英语单词链)

    构建之法》练习题

    大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。最长的定义是:最多单词数量,和单词中字母的数量无关。

    例如, 文件里有:

    Apple

    Zoo

    Elephant

    Under

    Fox

    Dog

    Moon

    Leaf

    Tree

    Pseudopseudohypoparathyroidism

     

    最长的相连英语单词串为:  apple - elephant – tree,  输出到文件里面,是这样的:

                  Apple

                  Elephant

                  Tree

     

    追加:判断文件不存在,文件无单词,文件一个单词,文件中无关联的(文件一个单词是其特殊情况)

    在文本input.txt中取出单词,在outout.txt显示最长的相连英语单词串

    任务分解:先行判断文件是否存在

    不存在->显示错误

    存在->取出单词存于内存中->判断单词数是否为一个

    一个->显示错误

    不是一个->判断相连英语单词(持续)(有一个判断是否重复的代码判断)->导出最长的相连英语单词串

    我的所有错误在控制台的输出都注释掉了,改成在文本里显示

    代码:

    package 六月六号;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Test1 {
    
    	public static void main(String[] args) throws IOException 
    	{
    		// TODO 自动生成的方法存根
    		String filename ="D://dada.txt";
    		File  a=new  File(filename);
    		File  file=new  File("D://output.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();    //获取结束时间
    	 System.out.println("程序运行时间:" + (endTime - startTime) + "ms");    //输出程序运行时间
    	}
    
    	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(" ");
    			//System.out.println(b1);
    			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("无互联词");
    		    }
    		}
    			
    			
    		  // for( String a:words)
    		  // {
    			 //  System.out.println(a);
    		//   }
    		 //  System.out.println(yao.toString()); 
    		   File file =new File("D://output.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("文件不存在");
            // try {
            //     file.createNewFile();
           //  } catch (IOException e) {
           // TODO Auto-generated catch block
          //      e.printStackTrace();      
         //   }
            return false;
        }
    }
    }
    

      

    优化分析:

    如果文件过大怎样解决:

    打开文件后,改为一个单词一个单词的读取的方式取代全部读出来的方式。这时需要判断单词间隔和单词首字母的大小写。俩个函数或者二者写在一起。

    你的最长的相连英语单词串可能会变得很长,此时也选择找出一个写进去一个的方式。

    我暂时只能想到这个问题的解决和分析,还有什么问题,希望大家提出来一起解决。

  • 相关阅读:
    BZOJ3514:GERALD07加强版(LCT,主席树)
    BZOJ2729:[HNOI2012]排队(组合数学)
    BZOJ4517:[SDOI2016]排列计数(组合数学,错排公式)
    BZOJ3123:[SDOI2013]森林(主席树,启发式合并)
    BZOJ3786:星系探索(Splay,括号序)
    BZOJ2212:[POI2011]Tree Rotations(线段树合并)
    BZOJ5329:[SDOI2018]战略游戏(圆方树,虚树)
    CF613D:Kingdom and its Cities(树形DP,虚树)
    BZOJ3611:[HEOI2014]大工程(树形DP,虚树)
    BZOJ2286:[SDOI2011]消耗战(树形DP,虚树)
  • 原文地址:https://www.cnblogs.com/gonT-iL-evoL-I/p/10986618.html
Copyright © 2011-2022 走看看