-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy path1111.cc
42 lines (40 loc) · 776 Bytes
/
1111.cc
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
//https://cses.fi/problemset/task/1111/
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
int main() {
string s;
cin >> s;
int n = s.size();
vi a(n),b(n);
for (int i=0,l=0,r=-1;i<n;i++) {
int k=i>r?1:min(a[l+r-i],r-i);
while (i>=k&&i+k<n&&s[i-k]==s[i+k]) k++;
a[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(b[l+r-i+1],r-i+1);
while (i>=k+1&&i+k<n&&s[i-k-1]==s[i+k]) k++;
b[i]=k--;
if(i+k>r) {
l=i-k-1;
r=i+k;
}
}
int m=0,k;
for (int i=0;i<n;i++) {
if(a[i]>m){
m=a[i];
k=i;
}
if(b[i]>=m){
m=b[i];
k=i;
}
}
cout<<(a[k]>b[k]?s.substr(k-a[k]+1,a[k]*2-1):s.substr(k-b[k],b[k]*2))<<endl;
}