3901 - Editor Asia - Seoul - 2007/2008

来源:互联网 发布:中超球员数据 编辑:程序博客网 时间:2024/06/05 15:50

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

 

 

/epsfbox{p3901.eps}

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 $ /sum$ 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

 


Seoul 2007-2008

 

 

 

#include<iostream>

#include<cstdio>

#include<string>

#include<vector>

using namespace std;

int main()

{

    int t;

    scanf("%d",&t);

    while(t--)

    {

        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;

}

原创粉丝点击