-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplalindrome.cpp
64 lines (59 loc) · 1.42 KB
/
plalindrome.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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 5005000
#define palin(x, id, c) (str[id - 1 - l[x]] - 'a' == c)
char str[MAXN], ans[MAXN];
struct Trie
{
int sz, odd_, even, last;
int next[MAXN][2], rt[MAXN], l[MAXN];
void Init()
{
sz = 0;
next[sz][0] = next[sz][1] = 0;
l[sz]= -2, rt[sz] = 0;
odd_ = ++sz, l[odd_] = -1, rt[odd_] = odd_;
even = ++sz, l[even] = 0, rt[even] = odd_;
last = odd_;
}
void Add(int c, int id)
{
while(!palin(last, id, c)) last = rt[last];
if (next[last][c]) last = next[last][c];
else
{
int x = last;
++sz;
next[x][c] = sz, l[sz] = l[x] + 2;
if (x == odd_) rt[sz] = even;
else
{
x = rt[x];
while(x != odd_ && (!next[x][c] || !palin(x, id, c))) x = rt[x];
if (next[x][c] && palin(x, id, c)) rt[sz] = next[x][c];
else rt[sz] = odd_;
}
last = sz;
}
}
}pt;
int main()
{
gets(str+1);
pt.Init();
int len = strlen(str + 1);
for (int i = 1; i <= len; i++)
{
int tmp = pt.sz;
pt.Add(str[i] - 'a', i);
if (tmp != pt.sz) ans[i] = '1';
else ans[i] = '0';
}
ans[len + 1] = 0;
puts(ans + 1);
return 0;
}