• 输出最长字符串链


    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("没有首尾相连");
            }
            
        }
    }

    设计思路:

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

    截图:

  • 相关阅读:
    Fire and Motion[转载]
    HLSL2GLSL v0.9 Released
    CEGUI Release of 0.5.0 stable by CrazyEddie 6th November 2006
    MapInfo 连接Oracle
    MapInfo连接SQLServer
    视图的创建及使用(sql server 2005)
    MapInfo 建立永久表
    MapInfo Update Feature
    MapInfo导入.TAB和.mws的方法
    触发器的创建及使用(sqlserver 2000)
  • 原文地址:https://www.cnblogs.com/liyuchao/p/10995069.html
Copyright © 2020-2023  润新知