-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanachar.cpp
More file actions
56 lines (50 loc) · 1.27 KB
/
manachar.cpp
File metadata and controls
56 lines (50 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// hdoj上3068题AC代码,输入manachar算法模板题
#include<iostream>
#include<string>
#include<stdio.h>
#define TIME std::ios::sync_with_stdio(false);
// 注意由于要插入'#',所以没有使用string,而是全局设置了字符串s与p数组
const int MAX=110000+10;
char s[MAX*2];
int p[MAX*2];
using namespace std;
int manachar(){
memset(p,0,sizeof(p));
int len = strlen(s);
int id = 0;
int maxlen = 0;
// 防止偶数串,字符间穿插'#',但s[0]为'*'
for(int i = len;i >= 0;i--){
s[i+i+2] = s[i];
s[i+i+1] = '#';
}
s[0] = '*';
// manachar核心代码,O(n)复杂度下补充p数组,并求得maxlen
for(int i = 2;i < 2*len + 1;i++){
if(p[id] + id > i){
p[i] = p[2*id - i] < p[id]+id-i ? p[2*id - i] : p[id]+id-i;
}else{
p[i] = 1;
}
while(s[i - p[i]] == s[i + p[i]]){
p[i]++;
}
if(id + p[id] < i + p[i]){
id = i;
}
if(maxlen < p[i]){
maxlen = p[i];
}
}
// 得到的maxlen是半径长度,包括了插入的'#'
return maxlen-1;
}
int main() {
TIME
while (scanf("%s",s) != EOF) {
int ans = manachar();
cout << ans << endl;
}
system("pause");
return 0;
}