-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmancher.cpp
61 lines (61 loc) · 1.55 KB
/
mancher.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
struct Mancher {
char str[maxn];//start from index 1
int p[maxn];
char s[maxn];
int n;
void checkmax(int &ans,int b){
if(b>ans) ans=b;
}
inline int min(int a,int b){
return a<b?a:b;
}
void kp(){
int i;
int mx = 0;
int id;
for(i=1; i<n; i++){
if( mx > i )
p[i] = min( p[2*id-i], p[id]+id-i );
else
p[i] = 1;
for(; str[i+p[i]] == str[i-p[i]]; p[i]++) ;
if( p[i] + i > mx ) {
mx = p[i] + i;
id = i;
}
}
}
void pre()
{
int i,j,k;
n = strlen(s);
str[0] = '$';
str[1] = '#';
for(i=0;i<n;i++)
{
str[i*2 + 2] = s[i];
str[i*2 + 3] = '#';
}
n = n*2 + 2;
str[n] = 0;
}
void solve() // 求出所有的最长回文子串所在的区间
{
int & tot = M::n;
tot = 0;
for(int i = 2; i < n; i++)
{
if(i%2&&p[i]==1) continue;
if(i%2)
{
M::in[tot++] = M::node(i/2-p[i]/2+1,i/2+p[i]/2);
//printf("%d %d\n",i/2-p[i]/2+1,i/2+p[i]/2);
}
else
{
M::in[tot++] = M::node(i/2-(p[i]/2-1),i/2+(p[i]/2-1));
//printf("%d %d\n",i/2-(p[i]/2-1),i/2+(p[i]/2-1));
}
}
}
}task1;