-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ1c.cpp
127 lines (115 loc) · 2.99 KB
/
Q1c.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <bits/stdc++.h>
#include <climits>
using namespace std;
#define ll long long int
string str;
ll *crank, *pos;
ll *cnt, *nextr;
bool *bh, *b2h;
ll *height;
bool smaller_first_char(int a, int b){
return str[a] < str[b];
}
void init_arrs(ll n) {
crank = new ll[n];
cnt = new ll[n];
pos = new ll[n];
nextr = new ll[n];
bh = new bool[n];
b2h = new bool[n];
height = new ll[n];
}
void suffixSort(ll n){
for (ll i=0; i<n; ++i){
pos[i] = i;
}
sort(pos, pos + n, smaller_first_char);
for (ll i=0; i<n; ++i){
bh[i] = i == 0 || str[pos[i]] != str[pos[i-1]];
b2h[i] = false;
}
for (ll h = 1; h < n; h <<= 1){
ll buckets = 0;
for (ll i=0, j; i < n; i = j){
j = i + 1;
while (j < n && !bh[j]) j++;
nextr[i] = j;
buckets++;
}
if (buckets == n) break;
for (ll i = 0; i < n; i = nextr[i]){
cnt[i] = 0;
for (ll j = i; j < nextr[i]; ++j){
crank[pos[j]] = i;
}
}
cnt[crank[n - h]]++;
b2h[crank[n - h]] = true;
for (ll i = 0; i < n; i = nextr[i]){
for (ll j = i; j < nextr[i]; ++j){
ll s = pos[j] - h;
if (s >= 0){
ll head = crank[s];
crank[s] = head + cnt[head]++;
b2h[crank[s]] = true;
}
}
for (ll j = i; j < nextr[i]; ++j){
ll s = pos[j] - h;
if (s >= 0 && b2h[crank[s]]){
for (ll k = crank[s]+1; !bh[k] && b2h[k]; k++)
b2h[k] = false;
}
}
}
for (ll i=0; i<n; ++i){
pos[crank[i]] = i;
bh[i] |= b2h[i];
}
}
for (ll i=0; i<n; ++i){
crank[pos[i]] = i;
}
}
void calculateLCPs(ll n){
for (int i=0; i<n; ++i) crank[pos[i]] = i;
height[0] = 0;
for (int i=0, h=0; i<n; ++i){
if (crank[i] > 0){
int j = pos[crank[i]-1];
while (i + h < n && j + h < n && str[i+h] == str[j+h]) h++;
height[crank[i]] = h;
if (h > 0) h--;
}
}
}
string findPalindrome(string orgstr) {
ll orglen = orgstr.length();
string revstr = orgstr;
reverse(revstr.begin(), revstr.end());
str = orgstr+"#"+revstr;
ll newlen = str.length();
init_arrs(newlen);
suffixSort(newlen);
calculateLCPs(newlen);
ll reqht=0;
ll startptr=0;
for(int i=1;i<newlen;++i) {
if((height[i]>reqht)) {
if((pos[i-1]<orglen && pos[i]>orglen)||(pos[i]<orglen && pos[i-1]>orglen)) {
reqht=height[i];
startptr=pos[i];
}
}
}
string longpalin = str.substr(startptr,reqht);
return longpalin;
}
int main() {
string orgstr;
cin >> orgstr;
string palin = findPalindrome(orgstr);
cout << palin << endl;
return 0;
}