-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva11475.cpp
81 lines (61 loc) · 964 Bytes
/
uva11475.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
#include<cstdio>
#include<iostream>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<sstream>
#include<limits.h>
#include<list>
#include<queue>
#include<map>
#define Max 1000000
using namespace std;
int b[Max];
void kmpPreProcess(string P)
{
int m = P.length();
int i = 0, j = -1; b[0] = -1;
while(i < m)
{
while(j >= 0 && P[i] != P[j])
j = b[j];
i++; j++;
b[i] = j;
}
}
int kmpSearch(string P, string T)
{
int m = P.length();
int n = T.length();
int i = 0, j = 0;
while(i < n)
{
while(j >= 0 && T[i] != P[j])
j = b[j];
i++; j++;
if(i == n)
return i - j;
if(j == m)
{
j = b[j];
}
}
}
int main()
{
string s, rev;
while(true)
{
cin >> s;
if(cin.eof())
break;
rev.clear();
rev.append(s);
reverse(rev.begin(), rev.end());
kmpPreProcess(s);
int index = kmpSearch(rev, s);
s.append(rev.begin() + rev.length() - index, rev.end());
cout << s << endl;
}
return 0;
}