标签:
| Time Limit: 1MS | Memory Limit: 102400KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
| input | output |
|---|---|
abbbba |
111111 |
Source
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
using namespace std;
typedef long long int LL;
const int MAX=5*1e6;
const int maxn=4*1e6+5;
char str[MAX+5];
struct Tree
{
int next[maxn][2];
int fail[MAX+5];
int len[MAX+5];
int s[MAX+5];
int last,n,p;
int new_node(int x)
{
memset(next[p],0,sizeof(next[p]));
len[p]=x;
return p++;
}
void init()
{
p=0;
new_node(0);
new_node(-1);
last=0;n=0;
s[0]=-1;
fail[0]=1;
}
int get_fail(int x)
{
while(s[n-len[x]-1]!=s[n])
x=fail[x];
return x;
}
int add(int x)
{
x-='a';
s[++n]=x;
int cur=get_fail(last);
if(!(last=next[cur][x]))
{
int now=new_node(len[cur]+2);
fail[now]=next[get_fail(fail[cur])][x];
next[cur][x]=now;
last=now;
return 1;
}
return 0;
}
}tree;
char ans[MAX+5];
int main()
{
while(scanf("%s",str)!=EOF)
{
tree.init();
int i;
for( i=0;str[i];i++)
{
if(!tree.add(str[i]))
ans[i]='0';
else
ans[i]='1';
}
ans[i]='\0';
puts(ans);
}
return 0;
}URAL 2040 Palindromes and Super Abilities 2(回文树)
标签:
原文地址:http://blog.csdn.net/dacc123/article/details/51360205