您的位置:首页 > 其它

ural 1960 Palindromes and Super Abilities 题解

2015-04-12 00:19 387 查看
题意:给一个串s,按顺序一个个加入到序列里面。输出每次加入之后序列中的本质不同的回文串个数。

回文自动机模板题- -extend函数里面,如果那个if进去了,就代表多了一个本质不同的回文串。

#include<cstdio>
#include<cstring>
const int MAXN=100000+5;
const int SIGMA_SIZE=26;
struct Node{
int num,len;
Node* go[SIGMA_SIZE],*fail;
Node():fail(0) {num=len=0;memset(go,0,sizeof(go));}
};
Node mem[MAXN],*cur=mem;
Node* root0,*root1;
Node* last;
void init()
{
cur=mem;
root0=cur++;
root1=cur++;
root0->fail=root1;
root1->len=-1;
last=root1;
}
inline Node* newNode(int len)
{
cur->len=len;
return cur++;
}
char s[MAXN];
int p=0;
inline Node* getFail(Node* t)
{
while(s[p- t->len -1]!=s[p]) t=t->fail;
return t;
}
int ans=0;
inline void extend(int w)
{
++p;
Node* t=getFail(last);
if(!t->go[w])
{
Node* nt=newNode(t->len+2);
t->go[w]=nt;
if(t==root1) nt->fail=root0;
else nt->fail=getFail(t->fail)->go[w];
nt->num=nt->fail->num+1;
++ans;
}
last=t->go[w];
printf("%d",ans);
}
int main()
{
init();
scanf("%s",s+1);
int n=strlen(s+1);
s[0]='$';
for(int i=1;i<=n;++i)
{
extend(s[i]-'a');
if(i!=n) putchar(32);
else putchar(10);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: