Description You're given a string of lower-case Latin letters. Your task isto find the length of its longest substring that can be met in thestring at least twice. These occurrences can overlap (see sampletest 2). Input The first input line contains the string. It's guaranteed, thatthe string is non-empty, consists of lower-case Latin letters, andits length doesn't exceed 100. Output Output one number — length of the longest substring that can bemet in the string at least twice. Sample Input
Input
abcd
Output
0
Input
ababa
Output
3
Input
zzz
Output
2 |
题意:
在所给字符串中找出最少出现两次的最长子串的长度
#include<iostream> using namespace std; int main() { int i,j; int l,t,maxl; char str[105]; while(cin>>str) { maxl=0; l=strlen(str); for(i=0;i<l;i++)// 从str[0]向后开始枚举 { for(j=i+1;j<l;j++)//一个个向后比较 { t=0; while(str[i+t]==str[j+t]) t++;//当找到第一个时,同时后移 maxl=t>maxl?t:maxl; //保存将当前至少2次重复的最长字串 // 长度 } } cout<<maxl<<endl; } return 0; }