-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEPALIN.cpp
More file actions
98 lines (91 loc) · 1.73 KB
/
EPALIN.cpp
File metadata and controls
98 lines (91 loc) · 1.73 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<vector>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
// Z-Algorithm for Pattern Matching
// O(n+m)
inline vector<int> calculateZ(string inp)
{
int len = inp.length();
vector<int> Z(len);
int left = 0;
int right = 0;
for(int k = 1; k < len; k++)
{
if(k > right)
{
left = right = k;
while(right < len && inp[right] == inp[right-left])
{
right++;
}
Z[k] = right - left;
right--;
}
else
{
int k1 = k - left;
// inside the Z-Box
if(Z[k1] < right-k+1)
{
Z[k] = Z[k1];
//if the value is inside the Z-box then copy it
}
else
{
// otherwise do more comparisons
left = k;
while(right < len && inp[right] == inp[right-left])
{
right++;
}
Z[k] = right-left;
right--;
}
}
}
return Z;
// Z[i] denotes the length of the longest substring starting from i which is also the prefix of the string.
}
int main()
{
string s;
while(getline(cin,s))
{
int len = s.length();
string rev_s = s;
reverse(rev_s.begin(),rev_s.end());
string modif_s = rev_s + "$" + s;
int modif_len = modif_s.length();
vector<int> z = calculateZ(modif_s);
int max_palin_len = -1;
for(int i = 0; i < z.size(); i++)
{
if(z[i] && z[i] == modif_len-i)
{
max_palin_len = modif_len - i;
break;
}
}
if(max_palin_len == s.length())
cout << s << "\n";
else
{
if(max_palin_len == 1)
{
string tans = s.substr(0,s.length()-1);
reverse(tans.begin(),tans.end());
string ans = s + tans;
cout << ans << "\n";
continue;
}
string tans = s.substr(0,len-max_palin_len);
reverse(tans.begin(),tans.end());
string ans = s + tans;
cout << ans << "\n";
}
}
return 0;
}