zoukankan      html  css  js  c++  java
  • 计算最长英语链

    package yingyu;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class output {
    
        public static void main(String[] args) throws FileNotFoundException {
            File file = new File("D:\input.txt");// 读取文件
            if (!file.exists()) {// 如果文件打不开或不存在则提示错误
                System.out.println("您的D盘文件不存在");
                return;
            }else {
                if(file.exists() && file.length() == 0) {  
                    System.out.println("您的D盘文件为空!");  
                    return;
                }  
            }
            long startTime = System.currentTimeMillis();
            String[] strs=new String[1000000];
            Scanner x = new Scanner(file);
            int i=0;
            boolean flag=false;
            while(x.hasNextLine()) {
                String[] str=x.nextLine().split("\W+");
                for(int ms=0;ms<str.length;ms++) {
                    if(!str[ms].equals("")&&str[ms].length()>2) {
                        flag=false;
    //                    System.out.println(str[ms]);
                        if(i!=0) {
                            for(int t=0;t<i;t++) {
                                if(!str[ms].equals(strs[t])) {
                                    flag=true;
                                }
                            }
                        }else {
                            flag=true;
                        }
                        
                        if(flag) {
                            strs[i]=str[ms];
                            i++;
                        }
                        
                    }
                    
                }
            }
            if(i==1) {
                System.out.println("该文件只有一个单词!无法实现词语接龙");
            }
            String sentence = "";
            String word="";
            String max="";
            for(int m=0;m<i;m++) {
                sentence = strs[m];
                word = sentence;
                for(int j=m+1;j<i;j++) {
                    if(strs[j].toLowerCase().subSequence(0, 1).equals(word.toLowerCase().subSequence(word.length()-1, word.length()))) {
                        word = strs[j];
                        sentence+="   "+word;
                    }
                }
                
                if(sentence.indexOf("   ")!=-1) {
                    if(sentence.length()>max.length()) {
                        max = sentence;
                    }
    //                System.out.println(sentence);
                }
                
            }
            FileWriter fw = null;
            try {
            //如果文件存在,则追加内容;如果文件不存在,则创建文件
            File f=new File("D:\output.txt");
            fw = new FileWriter(f, true);
            } catch (IOException e) {
            e.printStackTrace();
            }
            PrintWriter pw = new PrintWriter(fw);
            
        
        
            
            long endTime = System.currentTimeMillis();
        //    System.out.println(endTime-startTime+"ms");
            System.out.println(i);
            if(max.length()!=0) {
                //System.out.println(max);
                pw.print(max);
                pw.flush();
            }else {
                System.out.println("没有首尾相连");
            }
            
        }
    }

    实现英语单词接龙

  • 相关阅读:
    作业1
    113
    112
    工作量统计
    测试大纲
    2019-2020-2 20175335丹增罗布《网络对抗技术》Exp8 Web基础
    2019-2020-2 20175335丹增罗布 《网络对抗技术》Exp7 网络欺诈防范
    2019-2020-2 20175335丹增罗布 《网络对抗技术》Exp6 MSF基础应用
    2019-2020-2 20175335 丹增罗布 《网络对抗技术》Exp5 信息搜集与漏洞扫描
    2019-2020-2 20175335 丹增罗布《网络对抗技术》Exp4 恶意代码分析
  • 原文地址:https://www.cnblogs.com/baixiaoli/p/11059461.html
Copyright © 2011-2022 走看看