zoukankan      html  css  js  c++  java
  • 输出最长字符串链

    package com.English1;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class English1 {
        public static void main(String[] args) throws FileNotFoundException {
            File file = new File("D:\Eclipse workspace\English\input1.txt");// 读取文件
            if (!file.exists()) {// 如果文件打不开或不存在则提示错误
                System.out.println("文件不存在");
                return;
            }else {
                if(file.exists() && file.length() == 0) {  
                    System.out.println("文件为空!");  
                    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);
                }
                
            }
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime+"ms");
            System.out.println(i);
            if(max.length()!=0) {
                System.out.println(max);
            }else {
                System.out.println("没有首尾相连");
            }
            
        }
    }

    设计思路:

      首先从文件中读取,然后将单词写入到一个单词数组里,然后对数组进行操作。首先获取每个单词的第一个字母和最后一个字母,然后就判断是否是首尾相连,如果是首尾相连,再依次写进对象里面,最后将对象写进输出文件中。

    截图:

  • 相关阅读:
    创建并发布npm包
    ubuntu下python+tornado+supervisor+nginx部署
    Ubuntu下pycharm设定任务栏图标后打开出现问号图标
    Ubuntu下安装keras
    Windows和Ubuntu双系统
    Java获取精确到秒的时间戳
    Jmeter Java请求
    git 生成公钥、私钥方法与clone使用方法
    Oracle 安全管理
    五、python沉淀之路--字典
  • 原文地址:https://www.cnblogs.com/liyuchao/p/10995069.html
Copyright © 2011-2022 走看看