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

本文介绍了一种使用回文自动机解决特定字符串问题的方法。重点在于如何区分奇数和偶数长度的回文串,并通过实例演示了构建回文自动机的过程,以及如何高效地输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

http://blog.youkuaiyun.com/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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值