Ural 2040 Palindromes and Super Abilities 2 回文自动机

来源:互联网 发布:载软件.net framework 编辑:程序博客网 时间:2024/04/29 04:34

题目大意:给定一个字符串,从左到右依次加入每个字符,问每加入一个字符之后本质不同的回文串的数量增加多少

http://blog.csdn.net/huyuncong/article/details/41181953

回文自动机OTZ

注意:

1.这道题必须把奇串和偶串分开建 如果通过插入分隔符的方式建在一起会MLE

2.把长度为500W的01串一个一个输出会T掉 存在一个char数组里一起输出就行了

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 5000005using namespace std;int n;char s[M],ans[M];namespace Palindrom_Automaton{struct Trie{Trie *son[2],*fail;int dpt;Trie() {}Trie(int _):dpt(_) {}}mempool[M],*C=mempool,*odd=new (C++)Trie(-1),*even=new (C++)Trie(0),*last=odd;bool Extend(int x){Trie *p=last;while( s[x-p->dpt-1]!=s[x] )p=p->fail;if(p->son[s[x]-'a']){last=p->son[s[x]-'a'];return false;}last=p->son[s[x]-'a']=new (C++)Trie(p->dpt+2);if(p==odd)last->fail=even;else{for(p=p->fail;p!=odd&&s[x-p->dpt-1]!=s[x];p=p->fail);if(s[x-p->dpt-1]==s[x])last->fail=p->son[s[x]-'a'];elselast->fail=odd;}return true;}}int main(){using namespace Palindrom_Automaton;int i;gets(s+1);n=strlen(s+1);even->fail=odd;for(i=1;i<=n;i++){if(Extend(i))ans[i]='1';elseans[i]='0';}puts(ans+1);return 0;}


0 0