您的位置:首页 > 其它

[ural 1960] Palindromes and Super Abilities

2015-04-09 20:39 447 查看
继续练习模版。

回文树。。。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int Maxn=100005;
char S[Maxn];
int len[Maxn],ans[Maxn],fail[Maxn];
int son[Maxn][26],n,st,p,c,last,i,t;

int newnode(int x){
len[st] = x;
return st++;
}

void init(){
scanf("%s",S+1);
n = strlen(S+1);
S[0] = '#';
newnode( 0 );
newnode( -1 );
fail[0] = 1;
}

int get_fail(int x,int n){
while (S[n-len[x]-1]!=S
) x=fail[x];
return x;
}

int main(){
//freopen("ural.in","r",stdin);
//freopen("ural.out","w",stdout);
init();
for (i=1;i<=n;i++){
c = S[i]-'a';
t = get_fail(last,i);
if (son[t][c]==0){
p = newnode( len[t]+2 );
fail[p] = son[get_fail( fail[t], i)][c];
son[t][c] = p;
ans[i]=1;
}
last = son[t][c];
}
for (i=1;i<=n;i++) ans[i]+=ans[i-1];
for (i=1;i<n;i++) printf("%d ",ans[i]);
printf("%d\n",ans
);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: