-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlps.cpp
56 lines (51 loc) · 1.09 KB
/
lps.cpp
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
#include <bits/stdc++.h>
#define MAX 200005
using namespace std;
typedef unsigned long long hash;
const hash X = 33;
hash he[MAX];
hash hd[MAX];
hash x[MAX];
char s[MAX];
hash cv (char c){
return (c-'a'+1);
}
hash calce(int i, int j){
return (he[j+1] - he[i]*x[j-i+1]);
}
hash calcd(int i, int j){
return (hd[j+1] - hd[i]*x[j-i+1]);
}
int main(){
int n;
char c;
scanf("%d",&n);
for(int i = 1; i < 2*n; i += 2){
scanf(" %c",&s[i]);
s[i+1] = 'z'+2;
}
s[0] = 'z'+2;
s[2*n+1] = '\0';
x[0] = 1;
he[0] = hd[0] = 0;
for(int i = 1; i <= 2*n+1; i++){
x[i] = X*x[i-1];
he[i] = he[i-1]*X + cv(s[i-1]);
hd[i] = hd[i-1]*X + cv(s[2*n+1-i]);
}
int lo = 1, hi = n;
while(lo != hi){
int mid = (lo+hi+1)/2;
int aux = 2*mid+1;
int ok = 0;
for(int i = 0; i <= 2*n+1-aux; i++){
if(calce(i,i+aux-1) == calcd(2*n-(i+aux-1),2*n-i)){
ok = 1;
break;
}
}
if (ok) lo = mid;
else hi = mid-1;
}
printf("%d\n",lo);
}