-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSES_LongestPalindrome.cpp
More file actions
151 lines (122 loc) · 3.72 KB
/
Copy pathCSES_LongestPalindrome.cpp
File metadata and controls
151 lines (122 loc) · 3.72 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mxN = 1e6+10;
const int p1 = 29, p2 = 31;
const int mod1 = 1e9+7, mod2 = 1e9+7;
vector<int> pw1(mxN), pw2(mxN);
vector<int> invPw1(mxN), invPw2(mxN);
int n;
vector<pair<int,int>> hf, hr;
int modularInverse(int x, int mod){
int exp = mod-2;
int result = 1;
while(exp){
if(exp % 2) result = (x*result) % mod;
x = (x*x)%mod;
exp /= 2;
}
return result;
}
vector<pair<int,int>> hashing(string s){
vector<pair<int,int>> hsh(n);
for(int i=0; i<n; i++){
int cur1 = ((s[i] - 'a' + 1) * pw1[i]) % mod1;
int cur2 = ((s[i] - 'a' + 1) * pw2[i]) % mod2;
if(i){
cur1 = (cur1 + hsh[i-1].first) % mod1;
cur2 = (cur2 + hsh[i-1].second) % mod2;
}
hsh[i] = {cur1, cur2};
}
return hsh;
}
bool f(int x){
for(int i = x-1; i<n; i++){
int cut1 = i+1-x;
int cut2 = n-1-i;
pair<int,int> h1 = hf[i];
if(cut1 > 0){
h1.first = (h1.first - hf[cut1-1].first + mod1) % mod1;
h1.second = (h1.second - hf[cut1-1].second + mod2) % mod2;
h1.first = (h1.first * invPw1[cut1]) % mod1;
h1.second = (h1.second * invPw2[cut1]) % mod2;
}
int j = n-1-cut1;
pair<int,int> h2 = hr[j];
if(cut2 > 0){
h2.first = (h2.first - hr[cut2-1].first + mod1) % mod1;
h2.second = (h2.second - hr[cut2-1].second + mod2) % mod2;
h2.first = (h2.first * invPw1[cut2]) % mod1;
h2.second = (h2.second * invPw2[cut2]) % mod2;
}
if(h1 == h2) return true;
}
return false;
}
void solve(){
string s; cin>>s;
n = s.size();
hf = hashing(s);
reverse(s.begin(), s.end());
hr = hashing(s);
int result = 0;
int low = 0, high = n/2;
while(high - low > 1){
int mid = (high + low) / 2;
if(2*mid + 1 <= n and f(2*mid + 1)) low = mid;
else high = mid-1;
}
if(f(2*high + 1)) result = max(result, 2*high + 1);
else result = max(result, 2*low + 1);
low = 0, high = n/2;
while(high - low > 1){
int mid = (high + low) / 2;
if(2*mid <= n and f(2*mid)) low = mid;
else high = mid-1;
}
if(f(2*high)) result = max(result, 2*high);
else result = max(result, 2*low);
string ans;
for(int i = result-1; i<n; i++){
int cut1 = i+1-result;
int cut2 = n-1-i;
pair<int,int> h1 = hf[i];
if(cut1 > 0){
h1.first = (h1.first - hf[cut1-1].first + mod1) % mod1;
h1.second = (h1.second - hf[cut1-1].second + mod2) % mod2;
h1.first = (h1.first * invPw1[cut1]) % mod1;
h1.second = (h1.second * invPw2[cut1]) % mod2;
}
int j = n-1-cut1;
pair<int,int> h2 = hr[j];
if(cut2 > 0){
h2.first = (h2.first - hr[cut2-1].first + mod1) % mod1;
h2.second = (h2.second - hr[cut2-1].second + mod2) % mod2;
h2.first = (h2.first * invPw1[cut2]) % mod1;
h2.second = (h2.second * invPw2[cut2]) % mod2;
}
if(h1 == h2){
ans = s.substr(cut2, result);
break;
}
}
cout<<ans<<endl;
}
signed main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
optimize();
pw1[0] = pw2[0] = 1;
invPw1[0] = invPw2[0] = 1;
for(int i=1; i<mxN; i++){
pw1[i] = (pw1[i-1] * p1) % mod1;
pw2[i] = (pw2[i-1] * p2) % mod2;
invPw1[i] = modularInverse(pw1[i], mod1);
invPw2[i] = modularInverse(pw2[i], mod2);
}
solve();
return 0;
}