3901 - Editor Asia - Seoul - 2007/2008
2010-08-14 21:39
405 查看
3901 - Editor Asia - Seoul - 2007/2008 | ||||
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 alphabetis 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;
}
相关文章推荐
- Asia - Seoul - 2007/2008 UVA 3902 Network
- 3900 - Molar mass Asia - Seoul - 2007/2008
- 3904 - Tile Code Asia - Seoul - 2007/2008
- [Asia - Hefei - 2008/2009][B:Discrete Square Roots][数论基础]
- Win7、Windows Server 2008下无法在Windows Service中打开一个已经存在的Excel 2007文件问题的解决方案
- UVALive 4264 Message(Regionals 2008 :: Asia - Taipei+模拟)
- b2bf 分子量(Molar Mass, ACM/ICPC Seoul 2007, UVa1586)
- 2007->2008
- 2007-2008
- 在Windows Server 2008中安装Exchange Server 2007 SP1
- Visual Studio 2008 VSTO and InfoPath 2007 error when trying to run debug(转)
- 2015 UESTC Winter Training #4【Regionals 2008 :: Asia - Tehran】
- 让 SCOM 2007 R2 使用 SQL Server 2008 R2 数据库
- Windows Server 2008上安装 Exchange 2007 SP1
- HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)
- MOSS 2007/WSS 3.0运行在windows server 2008上不能上传大于28M的文件
- Visio 2007 反向 SQL Server 2008 工程
- 2007-2008(1)课程
- hdu 2473 Junk-Mail Filter(并查集_虚节点)2008 Asia Regional Hangzhou