-
Notifications
You must be signed in to change notification settings - Fork 0
/
11475.cpp
70 lines (68 loc) · 1.1 KB
/
11475.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
/*
11475 - Extend to Palindrome
*/
#include <bits/stdc++.h>
using namespace std;
int p_t[1000005];
void failure(string pattern);
int kp(string st,string stt);
int main()
{
int t,n,id=1,k,m;
string st,stt;
//cin>>t;
while(cin>>st)
{
string stt=st;
reverse(stt.begin(),stt.end());
failure(stt);
k=kp(st,stt);
stt=st.substr(0,st.size()-k);
reverse(stt.begin(),stt.end());
st+=stt;
cout<<st<<endl;
}
return 0;
}
void failure(string pattern)
{
int i,j,k,sz=pattern.size();
j=0;
for(i=1; i<sz;)
{
while(j && pattern[j]!=pattern[i])
j=p_t[j-1];
if(pattern[j]==pattern[i])
j++;
p_t[i]=j,i++;
}
}
int kp(string st,string stt)
{
int i,j,k,sz_st=st.size();
i=0,j=0;
while(1)
{
if(i==sz_st)
break;
if(st[i]==stt[j])
i++,j++;
else if(j)
j=p_t[j-1];
else
i++;
}
return j;
}
/*
Sample Input
aaaa
abba
amanaplanacanal
xyz
Sample Output
aaaa
abba
amanaplanacanalpanama
xyzyx
*/