-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathLongest Palindrome.cpp
47 lines (37 loc) · 986 Bytes
/
Longest Palindrome.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
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6+5;
char S[maxN];
int N, bestl, bestr, d1[maxN], d2[maxN];
int main(){
scanf(" %s", S);
N = (int) strlen(S);
for(int i = 0, l = 0, r = -1; i < N; i++){
int k = (i > r ? 1 : min(d1[l+r-i], r-i+1));
while(0 <= i-k && i+k < N && S[i-k] == S[i+k]) k++;
if(2*k-1 > bestr-bestl+1){
bestl = i-k+1;
bestr = i+k-1;
}
d1[i] = k--;
if(i+k > r){
l = i-k;
r = i+k;
}
}
for(int i = 0, l = 0, r = -1; i < N; i++){
int k = (i > r ? 0 : min(d2[l+r-i+1], r-i+1));
while(0 <= i-k-1 && i+k < N && S[i-k-1] == S[i+k]) k++;
if(2*k > bestr-bestl+1){
bestl = i-k;
bestr = i+k-1;
}
d2[i] = k--;
if(i+k > r){
l = i-k-1;
r = i+k;
}
}
for(int i = bestl; i <= bestr; i++)
printf("%c", S[i]);
}