You're Given a String...
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).
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 one number — length of the longest substring that can be met in the string at least twice.
Input
abcd
Output
0
Input
ababa
Output
3
Input
zzz
Output
2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
int main(){
string s;
cin >> s;
int i,j,k;
int longest = 0;
for(i = 0; i < s.length(); i++){
for(j = i; j < s.length(); j++){//选择出子串i到j
int index = i;
int cnt = 0;
for(k = 0; k < s.length(); k++){//从头比较
int q = 0;
int flag = 1;
while(q < (j-i+1)){//从k起点,然后往后长度为j-i+1,比较已经选出的子串,比较完后起始点挪到下一个
if(s[i+q] != s[k+q]){
flag = 0;
break;
}
else
q++;
}
if(flag)
cnt++;
}
if(cnt >= 2){
longest = max(longest,j-i+1);
}
}
}
cout << longest << endl;
return 0;
}
本文介绍了一道CodeForces上的题目,要求找出字符串中最长的重复子串长度。通过四层循环暴力求解的方式,最终实现了正确的解答。
594

被折叠的 条评论
为什么被折叠?



