Editor 字符串中是否有两个完全相同的子串-可重复

3901 - Editor
Asia - Seoul - 2007/2008
PDF   Submit   Ranking

 


 
Mr. Kim is a professional programmer. Recently he wants to design a new editor which has as many functions as possible. Most editors support a simple search function that finds one occurrence (or all occurrences successively) of a query pattern string in the text.

He observed that the search function in commercial editors does nothing if no query pattern is given. His idea of a new search function regards each substring of the given text as a query pattern string itself and his new function finds another occurrence in the text. The problem is that there can be occurrences of many substrings in the text. So, Mr. Kim decides that the new function finds only occurrences of the longest substring in the text in order to remedy the problem. A formal definition of the search function is as follows:

Given a text string S , find the longest substring in text string S such that the substring appears at least twice. The two occurrences are allowed to overlap.


Input 
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. For each test case, a text string S is given in one line. For every string, the length is less than or equal to 5,000 and the alphabet  is the set of lowercase English characters.


Output 
Your program is to write to standard output. Print exactly one line for each test case. Print the length of the longest substring in text string S such that the substring appears at least twice.


Sample Input 

3
abcdefghikjlmn
abcabcabc
abcdabcabb


Sample Output 

0
6
3


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        vector<int> pre[26];
        string str;cin>>str;
        int len=str.size();
        for(int i=0;i<len;i++) pre[str[i]-'a'].push_back(i);
        int maxn=0;
        for(int i=0;i<26;i++)
        {
            int l=pre[i].size();
            if(l<=1) continue;
            for(int j=0;j<l;j++)
            {
                for(int k=j+1;k<l;k++)
                {
                    int cnt=0;
                    int g=pre[i][j],h=pre[i][k];
                    for(int c=0;h+c<len;c++,cnt++)
                    {
                        if(str[g+c]!=str[h+c]) break;
                    }
                    if(cnt>maxn) maxn=cnt;
                }
            }
        }
        printf("%d/n",maxn);
    }
    return 0;
}                       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值