首页 > 其他 > 详细

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

时间:2015-03-26 14:58:00      阅读:223      评论:0      收藏:0      [点我收藏+]

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

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 5000005
using 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'];
			else
				last->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';
		else
			ans[i]='0';
	}
	puts(ans+1);
	return 0;
}


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

原文:http://blog.csdn.net/popoqqq/article/details/44648537

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!