-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathKMP.cpp
41 lines (34 loc) · 768 Bytes
/
KMP.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
//SPOJ EPALIN
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1e5+5, M = 2e5+5, OO = 0x3f3f3f3f;
int n;
char str[N], pat[M];
int F[M];
int getNextLen(int len, char c){
while(len && pat[len] != c)
len = F[len-1];
if(pat[len] == c) ++len;
return len;
}
void computeF(){
F[0] = 0;
for(int i = 1 ; i < n ; ++i)
F[i] = getNextLen(F[i-1], pat[i]);
}
int main(){
while(~scanf("%s", str)){
strcpy(pat, str);
reverse(pat, pat+strlen(pat));
strcat(pat, "#");
strcat(pat, str); //pat = reversedString + # + givenString
n = strlen(pat);
computeF();
printf("%s", str);
for(int i = strlen(str)-1-F[n-1] ; ~i ; --i) printf("%c", str[i]);
puts("");
}
return 0;
}