您的位置:首页 > 其它

codeforences 29A

2014-04-08 20:10 501 查看
A - You're Given a String... p
Time Limit: 2000MS Memory Limit: 262144K 64bit IO Format: %I64d & %I64u
[Submit]   [GoBack]   [Status]DescriptionYou're given a string of lower-case Latin letters. Your task is to find the length of its longest substring that can be met in the string at least twice. These occurrences can overlap (see sample test 2).InputThe first input line contains the string. It's guaranteed, that the string is non-empty, consists of lower-case Latin letters, and its length doesn't exceed 100.OutputOutput one number — length of the longest substring that can be met in the string at least twice.Sample InputInput
abcd
Output
0
Input
ababa
Output
3
Input
zzz
Output
2
这个题目的意思是说找出其子串中最大循环的个数,我很惭愧,在kmmm的这个例子里,应该是2,可我一开始认为是0.是子串,可以包含没有的,所以不可以用kmp,因为kmp数组求出的是前面的最大匹配数目,这个时候有人可能会问假如遍历next[],数组求出最大的不就行了吗,其实不行的,因为在aaa,中,next[]最大是3,而其实是2的,所以暴力比较是个不错的法子。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char s[120];int main(){int i,j,k;int max;while(scanf("%s",s)!=EOF){max=-100;int l=strlen(s);for(i=0;i<l;i++){for(j=i+1;j<l;j++){k=0;while(s[i+k]==s[j+k]){k++;}if(max<k)max=k;}}printf("%d\n",max);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  substring 遍历 kmp string