• 计算最长英语单词链


     设计思路:

    这个问题,首先要找出每个单词的首尾字母,通过判断首尾相同的字母连接在一起;

    如何存每个单词的首尾字母?这里借鉴同学的方法:建立两个字符串数组,一个放每一个单词的首字母,另一个放每一个单词的尾字母;

    然后根据这两个是否相同,放进一个int数组中

    源代码:

    import java.io.BufferedWriter;

    import java.io.File;

    import java.io.FileReader;

    import java.io.FileWriter;

    import java.io.IOException;

    public class Max {

    public static void main(String[] args)throws IOException {

        Word word=new Word();                                      //单词的链头

        Word lian,xin;                                             

        String str="";

        File f1=new File("D:\text.txt");      

        if(!f1.exists())

        {

            System.out.println("文件不存在");

        }

        else

            {FileReader f=new FileReader("D:\text.txt");                //读取英文文件

        

        

        char[] c=new char[1];                                 //每次读取一个字母

        int b=0;

        boolean exist=false;                              //判断单词是否存在于  word 链中

       

        BufferedWriter writer2 = new BufferedWriter(new FileWriter(new File("D:\result.txt"),true));

        

        File writeName=new File("D:\result.txt");

        String S1="";

        String S2="";

        writeName.createNewFile();

      

        int num1=0;

        int num2=0;

        int[] Num1=new int[1000000];

        int[] Num2=new int[1000000];

        

        

        

        while((b=f.read(c))!=-1)                              //每次读取一个字母直到最后

        {

            int i1=1;

            //如果字符为  换行、空格、单引号、双引号、逗号、句号  则为一个单词的结束及另一个单词的开始

            if(String.valueOf(c).equals(" ")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals(""")||String.valueOf(c).equals("'"))

            {

                lian=word;

                while(lian!=null)            

                {

                    if(lian.value.equalsIgnoreCase(str))           //如果单词在单词链中存在,则单词个数++

                    {

                        lian.geshu++;exist=true;break;

                    }

                    else

                    {

                        if(i1==1)

                        {

                            

                        

                        Num1[num1]=num1;

                        

                        String ss=str.substring(0, 1);

                        

                        S1=S1+ss;

                        num1=num1+1;

                        }

                        

                        if(i1==1)

                        {

                            Num2[num2]=num2;

                            String ss=str.substring(str.length()-1, str.length());

                            

                            S2=S2+ss;

                            num2=num2+1;

                        }

                        

                        

                        i1=i1+1;

                        lian=lian.next;

                    }

                }

                if(exist==false)                        //如果不存在,则在单词链中添加

                {

                    xin=new Word(str,1);

                    xin.next=word.next;

                    word.next=xin;

                    str="";

                }

                else

                {

                    exist=false;

                    str="";

                }

            }

            else                                      //单词

            {

                str+=String.valueOf(c);

            }

            

        }

        

        if(num1==1)

        {

            System.out.println("只有一个单词");

        }

        else {

        

            if(num1==0)

            {

                System.out.println("没有单词");

            }

            

            

            else {

            

            

        System.out.println(S1);

        System.out.println(S2);

        

        System.out.println(num1);

        for(int im=0;im<num1;im++)

        {

            String s1=S1.substring(im, im+1);

            int ii=1;

            for(int in=0;in<num1;in++)

            {

                

                String s2=S2.substring(in, in+1);

                

                int n1=Num1[im];

                int n2=Num2[im];

                

                if(!s1.equals(s2))

                {

                    System.out.println("没有首尾相连单词");

                }

                    

                    

                if(s1.equals(s2))

                {

                    

                    

                    

                    

                    int N=20;

                    for(int i=1;i<=10;i++)                   

                    {

                        xin=new Word("",0);

                        lian=word.next;

                        //找到单词链中个数最多的

                        while(lian!=null)

                        {

                            

                                xin=lian;

                            

                            lian=lian.next;

                        }

                        int m=n1-1;

                        if(i==n1-1)

                        {

                            

                             System.out.println(""+i+":"+xin.value+"个数:"+xin.geshu);

                        

                             try  {

                                 

                                 BufferedWriter writer = new BufferedWriter(new FileWriter(new File("D:\result.txt"),true));

                                 writer.write(" "+xin.value);

                                 writer.close();

                                    

                                }

                             catch (IOException e) {

                                e.printStackTrace();

                            }

                

                        

                        }

                        lian=word;

                        //删除单词链中单词个数最多的

                        while(lian.next!=null)

                        {

                            if(lian.next.value.equalsIgnoreCase(xin.value))

                            {

                                lian.next=lian.next.next;

                                break;

                            }

                            lian=lian.next;

                        }

                    }

                    

                }

            }

            

            ii=ii+1;

            

            

        }

        

               }

        }

        

            }

    }

    }

     摘自https://www.cnblogs.com/smartisn/p/10989038.html

  • 相关阅读:
    npm 操作
    vue 下拉框选中不显示的问题
    VUE项目中遇到的Bug总结
    Websocket练手
    Mysql 去重取最新的一条数据
    C# 一列数的规则如下:1、1、2、3、5、8、13..…...求第30位数是多少,用递归算法实现
    冒泡排序 升序 降序(笔试)
    Sql语句查询成绩大全(Mysql,sqlserver,oracle)常遇笔试题
    C# ,.net 对比两个List的方法 亲测
    C#当前时间加、减指定时间
  • 原文地址:https://www.cnblogs.com/sengzhao666/p/11058148.html
Copyright © 2020-2023  润新知