-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtendToPalindromes314.cpp
More file actions
64 lines (64 loc) · 1.09 KB
/
ExtendToPalindromes314.cpp
File metadata and controls
64 lines (64 loc) · 1.09 KB
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 <iostream>
#include <string>
using namespace std;
void kmp(string pat, string text, int* lps) {//returns the index at which pattern is found
int m = pat.length();
int n = text.length();
bool found = false;
int i = 0;
int j = 0;
while (i < n) {
if (pat[j] == text[i]) {
i++;
j++;
}
if (j == m) {
j = lps[j - 1];
found = true;
}
else if (i<n && pat[j]!=text[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
cout << text;
if (!found) {
for (int k = j; k < pat.length(); k++)
cout << pat[k];
}
cout << endl;
}
void computeFailureFunction(string pat, int n, int* lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < n) {
if (pat[i] == pat[len]) {
len++;
lps[i++] = len;
}
else {
if (len != 0) {
len = lps[len - 1];
}
else {
lps[i++] = 0;
}
}
}
}
int main() {
int f[100001];
string input;
while (cin >> input) {
string revstr = "";
for (int i = input.length()-1; i >= 0; i--) {
revstr += (char)input[i];
}
computeFailureFunction(revstr, revstr.length(), f);
kmp(revstr, input, f);
}
return 0;
}