-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD2.cpp
268 lines (227 loc) · 6.14 KB
/
D2.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> pll;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,b,a) for(int i=b;i>a;i--)
#define FORDE(i,b,a) for(ll i=b;i>=a;i--)
#define debug(x) cout<< '>'<<#x<<" : "<<x<<"\n";
#define debug2(x,y) cout<< '>'<<#x<<" : "<<x<<"\n"; cout<< '>'<<#y<<" : "<<y<<"\n";
#define debug3(x,y,z) cout<< '>'<<#x<<" : "<<x<<"\n"; cout<< '>'<<#y<<" : "<<y<<"\n";cout<< '>'<<#z<<" : "<<z<<"\n";
#define umap unordered_map
#define uset unordered_set
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define in insert
#define s second
#define f first
#define nn cout<<"\n"
#define pb push_back
#define testcase int t;cin>>t;while(t--)
#define gcd(a,b) __gcd(a,b)
#define maxv INT_MAX
#define minv INT_MIN
#define MOD 1000000007
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(0);
#define here cout<<"I'm here\n";
#define flush fflush(stdout);
#define ordered_set_single tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
typedef tree<
pair<ll, ll>,
null_type,
less<pair<ll,ll>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set_pair;
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
ll modInverse(ll n, ll p)
{
return power(n, p-2, p);
}
ll nCrModPFermat(ll n, ll r, ll p)
{
// Base case
if (r==0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
ll fac[n+1];
fac[0] = 1;
for (ll i=1 ; i<=n; i++)
fac[i] = fac[i-1]*i%p;
return (fac[n]* modInverse(fac[r], p) % p *
modInverse(fac[n-r], p) % p) % p;
}
ll nCrModpDP(ll n, ll r)
{
ll C[r+1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (ll i = 1; i <= n; i++)
{
for (ll j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j-1])%MOD;
}
return C[r];
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
ll ntopowermandMod(ll n,ll m,ll mod_v)
{
if(m==0)
{
return 1;
}
if(m==1)
{
return n;
}
else
{
ll val=ntopowermandMod(n,m/2,mod_v);
val=val%mod_v;
val=val*val;
val=val%mod_v;
if(m%2==1)
{
val=val*n;
val=val%mod_v;
}
return val;
}
}
ll ntopowerm(ll n,ll m)
{
if(m==0)
{
return 1;
}
if(m==1)
{
return n;
}
else
{
ll val=ntopowerm(n,m/2);
val=val*val;
if(m%2==1)
{
val=val*n;
}
return val;
}
}
vector<int> pi;
int prefix_function(string s) {
int n = (int)s.length();
int v1=0;
for (int i = 1; i < n; i++) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
j++;
pi[i] = j;
if(pi[i]>0)
v1=i;
}
return v1;
}
template<class T> void dispvector(vector<T> v){for(int i=0;i<v.size();i++) cout<<v[i]<<" "; nn;}
template<class T> void disparray(T *v, int n){for(int i=0;i<n;i++) cout<<v[i]<<" "; nn;}
template<class T> int sizeofarr(T *v){return sizeof(v)/sizeof(T);}
signed main(int argc, char** argv)
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
FastIO;
long t;
cin>>t;
while(t--)
{
string str;
cin>>str;
ll n=str.size();
ll st=0;
ll end=n-1;
string answ="";
while(st<end && str[st]==str[end])
{
st++;
end--;
}
if(st>=end)
{
cout<<str<<endl;
continue;
}
else{
string s=str;
ll po=st;
ll qo=end;
string ans = s.substr(0, po), ansp = ans;
string f = s.substr(po, qo-po+1);
for(int i = 0; i < f.size()/2; i++)
{
swap(f[i], f[f.size()-i-1]);
}
string w = s.substr(po, qo-po+1) + "*" + f;
pi.clear();
for(int i = 0; i < w.size(); i++)pi.push_back(0);
prefix_function(w);
for(int i = 0; i < pi[w.size()-1]; i++){
ans.push_back(w[i]);
}
for(int i = qo+1; i < n; i++){
ans.push_back(s[i]);
}
string w2 = f + "*" + s.substr(po, qo-po+1);
pi.clear();
for(int i = 0; i < w2.size(); i++)pi.push_back(0);
prefix_function(w2);
for(int i = 0; i < pi[w2.size()-1]; i++){
ansp.push_back(w2[i]);
}
for(int i = qo+1; i < n; i++){
ansp.push_back(s[i]);
}
if(ansp.size() > ans.size())ans = ansp;
cout << ans << '\n';
}
}
return 0;
}