# bzoj 2084: Antisymmetry 回文自动机

Description

Input

Output

## 题解:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch = getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 500010;
struct Node{
int nx[2];
int len,fail,siz;
}T[maxn];
int nodecnt,last,s[maxn],len;
inline void init(){
T[last = nodecnt = 0].fail = 1;
T[++nodecnt].len = -1;
}
inline void insert(int c){
s[++len] = c;int cur,p,x;
for(p = last;p != 1 && (s[len-T[p].len-1] == s[len] || (len-T[p].len-1 == 0));p = T[p].fail);
if((s[len-T[p].len-1] == s[len] || (len-T[p].len-1 == 0))){last = 0;return;}
if(T[p].nx[c] == 0){
T[cur = ++ nodecnt].len = T[p].len + 2;
for(x = T[p].fail;x != 1 && (s[len-T[x].len-1] == s[len] || (len-T[p].len-1 == 0));x = T[x].fail);
if(s[len-T[x].len-1] == s[len]) T[cur].fail = 0;
else T[cur].fail = T[x].nx[c];
T[p].nx[c] = cur;
}T[last = T[p].nx[c]].siz ++ ;
}
int main(){
init();
for(int i=1;i<=n;++i){
while(ch=getchar(),ch<'!');
insert(ch - '0');
}
for(int i=nodecnt;i>=2;--i){
if(T[i].fail) T[T[i].fail].siz += T[i].siz;
}int ans = 0;
for(int i=2;i<=nodecnt;++i) ans += T[i].siz;
printf("%d\n",ans);
getchar();getchar();
return 0;
}


posted @ 2017-03-13 07:08  Sky_miner  阅读(576)  评论(0编辑  收藏  举报