-
Notifications
You must be signed in to change notification settings - Fork 0
/
1326D1.cpp
64 lines (61 loc) · 1.5 KB
/
1326D1.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
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define LL long long
#define mod 998244353
#define FOR(i, j, k) for (int i=j ; i<k ; i++)
#define ROF(i, j, k) for (int i=j ; i>=k ; i--)
const long long INF = 1e18;
const long long MAX = 1e5+10;
void lp(vector<int> &v, int n, string s){
int i=0,j=1; v[i]=0;
while(j<n){
if(s[i]==s[j]){
v[j]=++i;
j++;
}
else {
if(i!=0){
i = v[i-1];
}
else {
v[j]=0;
j++;
}
}
}
}
int main(){
fastio;
int t; cin>>t;
while(t--){
string s,p; cin>>s;
int i=0,n=s.size();
while(i<=n/2 && s[i]==s[n-i-1]) i++;
if(n-2*i>0) p= s.substr(i,n-2*i);
else {
cout<<s<<"\n";
continue;
}
string rv = p;
reverse(rv.begin(),rv.end());
string s1 = p+'@'+rv,s2=rv+'@'+p;
n=s1.size();
vector<int>v1(n),v2(n);
lp(v1,n,s1);
lp(v2,n,s2);
int m1=v1[n-1],m2=v2[n-1];
//cout<<m1<<" "<<m2<<" "<<s1<<" ";
if(i==0 && m1==0 && m2==0) cout<<s[0];
else if(m1>=m2){
FOR(j,0,i) cout<<s[j];
FOR(j,0,m1) cout<<s1[j];
ROF(j,i-1,0) cout<<s[j];
} else {
FOR(j,0,i) cout<<s[j];
FOR(j,0,m2) cout<<s2[j];
ROF(j,i-1,0) cout<<s[j];
}
cout<<"\n";
}
}