在一个字符串中寻找到最长重复子串(1)

本题应该从超过100w的字符串中寻找,但是本算法执行到1000时就比较慢了。时间空间复杂度不好,可以很方便的计算出来。如果您有好的方法可以分享一下


其中包含:随进输出字符串,文件读取


算法描述:母串有n个字符,最大的子串最多有n/2,所以:

1、把母串分为长度相等的两部分,进行比较

2、长度减1,在进行比较

3、假设已经比较过m次,剩下的字符串为n-m,为k。先取母串的前k个字符,与k+1到2K之间的字符串进行比较,再与k+2到2k+1比较,一直到n-k到n。然后第一个串后移一位,重复上述过程。



package mainpackage;


import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class A {

    static public void main(String args[]) {
         //String s = new String("xouahbglcemdohpcfllcdjvmsrohyjfogbqbgwgtibuzchhmlztfnakhykryoeczgjdufmvuljlcslmvirulltqzydgctvqeddlf");
        //String ss = new String("sabcwabcb");rylxhbdmydhdtlzupzxphhhgwgiu

        //String s = "xasccascv";
        // System.out.println(s);
        
        //产生随机的字符串
        String base = "abcdefghijklmnopqrstuvwxyz";
        Random random = new Random();
        StringBuffer str = new StringBuffer();
        String ss = new String();
        StringBuffer s = new StringBuffer();
        int length = 300;
        for(int i = 0;i < length; i++)
        {
            int number = random.nextInt(base.length());
            str.append(base.charAt(number));
        }
        ss = str.toString();
        System.out.println(ss);//测试输出字符串
        
        //创建文件
        File file = new File("f.txt");
        if(!file.exists())
        {
            try
            {
                file.createNewFile();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        
        //把字符串写入到文件
        //System.out.println(ss);
        try
        {
            FileWriter fw = new FileWriter("f.txt");
            fw.write(ss);
            fw.close();
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
        
        //从文件中读取
        try(FileReader fr = new FileReader("f.txt"))
        {
            char [] cbuf = new char[length];
            //int hasRead = 0;
            /*while((hasRead = fr.read(cbuf))>0)
            {
                s.append(cbuf);
            }*/
            fr.read(cbuf);
            s.append(cbuf);
            fr.close();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        //System.out.println(s);
        
        int string_length;
        string_length = s.length();
        //System.out.println(string_length);

        int out = 0  ;
        for (int i = string_length / 2; i >= 1; i--)// 从字符串长度的一半开始
        {
            if(out != 0)
            {
                break;
            }
            //System.out.println(i);
            String child_string1 = null;
            String child_string2 = null;
            for(int k = 0; k <= (string_length - i*2); k++)
            {
                if(out != 0)
                {
                    break;
                }
                for (int j = 0; j <= (string_length - i*2); j++)
                {
                    if(out != 0)
                    {
                        break;
                    }
                    if ((i * 2 + k + j) <= string_length)
                    {
                        child_string1 = s.substring(k, k + i);
                        //System.out.println(child_string1);
                        child_string2 = s.substring(k + i + j, k + i*2 + j);
                        //System.out.println(k + i*2 + j);
                        if (child_string1.equals(child_string2))
                        {
                            System.out.println("最大子串的长度为:" + child_string1.length()
                                    + ";子串为:" + child_string1);
                            out = 1;
                        }
                    }
                    else
                    {
                        j = string_length - i*2;//结束j的循环,所以赋值为string_length - i*2
                    }

                    if (out != 0) // 结束for循环
                    {
                        j = string_length - i*2 + 1;
                        k = string_length - i*2 + 1;
                        i = 0;    
                    }
                }
            }
        }
        //System.out.println(string_length);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值