You're Given a String... (kmp)
2018-02-09 16:10
375 查看
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).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.
Example Input
abcdOutput
0Input
ababaOutput
3Input
zzzOutput
2枚举子串,kmp匹配
code:#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
const int MAXN = 110;
string s;
string w;
int Next[MAXN];
void getNext(){
int i = -1,j = 0;
int len = s.length();
memset(Next,0,sizeof(Next));
Next[0] = -1;
while(j < len){
if(i == -1 || w[i] == w[j]){
i++,j++;
Next[j] = i;
}
else
i = Next[i];
}
}
int kmp(){
int i = 0, j = 0,cnt = 0;
int lens = s.length();
int lenw = w.length();
getNext();
while(j < lens){
if(i == -1 || w[i] == s[j])
i++,j++;
else
i = Next[i];
if(i == lenw)
cnt++;
}
return cnt;
}
int main(){
int i,j;
int ans = 0;
cin >> s;
for(i = 0; i < s.length(); i++){
for(j = i; j < s.length(); j++){
w = s.substr(i,j-i+1);
if(kmp() >= 2){
int len = j-i+1;
ans = max(ans,len);
}
}
}
cout << ans << endl;
return 0;
}
相关文章推荐
- 【strstr】#23 A. You're Given a String...
- CodeForces 23A-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 su
- A - You‘re Given a String
- 23A - You're Given a String...
- You're Given a String...
- You're Given a String... CodeForces - 23A
- A. You're Given a String...
- CodeForces 23A You're Given a String... (简单题)
- CareerCup Given a dictionary, how would you add spaces in this string
- codeforces 23A. You're Given a String...
- You’re given an array containing both positive and negative integers and required to find the sub-a
- 关于ExtJs4提交数据到jsp页面,返回正确的json显示例子。及error:You're trying to decode an invalid JSON String...解决方法
- How do you remove the duplicate characters in a given string without using any additional buffer.
- ie 浏览器 You're trying to decode an invalid JSON String的问题
- CodeForces 23A You're Given a String...(字符串处理)
- A. You\'re Given a String...
- Rearrange a string so that all same characters become d distance away
- Hdu-3336-Count the string-【KMP】
- Warning: number_format() expects parameter 1 to be double, string given in