-
Notifications
You must be signed in to change notification settings - Fork 2
/
HDU-dx11-1009.cpp
115 lines (108 loc) · 1.89 KB
/
HDU-dx11-1009.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
/*
ID: mfs6174
PROG: ti
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
//ifstream inf("ti.in");
//ofstream ouf("ti.out");
const int maxlongint=2147483647;
const int mod=1003001;
char s[110100];
int dp[220200], len;
bool hash[mod+10];
int i,j,k,t,n,m,zz,zu,cc,ll,rr;
char tc;
bool fl[300];
unsigned bkdrhash(char x[])
{
unsigned int seed=131,hash=0,i,l=strlen(x);
for (i=0;i<l;i++)
hash=(hash*seed+x[i])&0xffffffff;
return (hash&0x7FFFFFFF)%mod;
}
int solve()
{
int ret = 1, max = 1;
len = strlen(s);
int l, r, m;
dp[0] = 0;
dp[1] = 1;
for(int i = 2; i < 2 * len; ++i)
{
if(i / 2 >= dp[max])
{
r = i / 2;
l = i - 1 - r;
}
else
{
m = max * 2 - i;
if(dp[m] - m / 2 > dp[max] - i / 2)
{
r = dp[max];
l = i - 1 - r;
} else
{
l = max - 1 - dp[m];
r = i - 1 - l;
}
}
while(l >= 0 && r < len && s[l] == s[r]) --l, ++r;
dp[i] = r;
ret = r - l - 1;//这里就是找到的回文串的左端之前和右端
if (ret>1)
{
ll=l;rr=r;
while (rr-ll-1>1)
{
tc=s[rr];s[rr]=0;
t=bkdrhash(s+ll+1);
if (!hash[t])
{
cc++;
hash[t]=true;
}
else
{
s[rr]=tc;
break;
}
s[rr]=tc;
ll++;rr--;
}
}
if(r > dp[max]) max = i;
if((r >= len)) break;
}
return ret;
}
int main()
{
freopen("ti.in","r",stdin);
scanf("%d",&zu);
for (zz=1;zz<=zu;zz++)
{
scanf("%s",s);
memset(hash,0,sizeof(hash));
cc=0;memset(fl,0,sizeof(fl));
len=strlen(s);
for (i=0;i<len;i++)
if (!fl[s[i]])
{
fl[s[i]]=true;
cc++;
}
solve();
printf("Case #%d: %d\n",zz,cc);
}
return 0;
}