codeforences 29A

来源:互联网 发布:王伟看懂世界格局知乎 编辑:程序博客网 时间:2024/06/06 01:49
A - You're Given a String... p
Time Limit: 2000MS Memory Limit: 262144K 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]

Description

You'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).

Input

The 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.

Output

Output one number — length of the longest substring that can be met in the string at least twice.

Sample Input

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


0 0
原创粉丝点击