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;
}