forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaldr.cpp
79 lines (77 loc) · 1.18 KB
/
paldr.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
// 2010-11-15
// Thanks to Frederick Akalin
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
template <class RAI1,class RAI2>
void fastLongestPalindromes(RAI1 seq,RAI1 seqEnd,RAI2 out)
{
int seqLen=seqEnd-seq;
int i=0,j,d,s,e,lLen,k=0;
int palLen=0;
while (i<seqLen)
{
if (i>palLen && seq[i-palLen-1]==seq[i])
{
palLen+=2;
i++;
continue;
}
out[k++]=palLen;
s=k-2;
e=s-palLen;
bool b=true;
for (j=s; j>e; j--)
{
d=j-e-1;
if (out[j]==d)
{
palLen=d;
b=false;
break;
}
out[k++]=min(d,out[j]);
}
if (b)
{
palLen=1;
i++;
}
}
out[k++]=palLen;
lLen=k;
s=lLen-2;
e=s-(2*seqLen+1-lLen);
for (i=s; i>e; i--)
{
d=i-e-1;
out[k++]=min(d,out[i]);
}
}
int main()
{
static int V[2222222];
static char s[1111111];
int T;
scanf("%d",&T); while (getchar()!='\n');
while (T--)
{
int N;
N=strlen(gets(s));
if (s[N-1]=='\r') N--;
fastLongestPalindromes(s,s+N,V);
int i=0; //starting from here
while (i<N)
{
int j=i+1;
while (j<N&&V[2*j]<2*(j-i)) j++;
if (j==N) break;
else i=2*j-i;
}
if (i==N) puts("YES"); else puts("NO");
}
return 0;
}