AC自动机模板(摘自刘汝佳紫书,无指针)

本题的题目选择的是

病毒侵袭持续中

 HDU - 3065 

const int size=5e4+5;
const int SIGMA_SIZE=26;
int trie[size][26],val[size];
char s_save[1005][60],s_fin[2000005];
int f[size];
int last[size];
int tot=1;
int sum[1005];
void init()
{
	memset(trie,0,sizeof(trie));
	memset(val,0,sizeof(val));
	memset(f,0,sizeof(f));
	tot=1;
	memset(sum,0,sizeof(sum));
} 
void cal(int j)
{
	if(j)
	{
		sum[val[j]]++;
		cal(last[j]);
	}
}
void insert(int v,char *s_ins)
{
	int len=strlen(s_ins);
	int root=0;
	for(int i=0;i<len;i++)
	{
		int id=s_ins[i]-'A';
		if(!trie[root][id])
		{	
			memset(trie[tot],0,sizeof(trie[tot]));
			val[tot]=0;	
			trie[root][id]=tot++;
		}
		root=trie[root][id];
	}
	val[root]=v;
} 
void query()
{
	int len=strlen(s_fin);
	int j=0;
	for(int i=0;i<len;i++)
	{
		if(s_fin[i]<'A'||s_fin[i]>'Z')
		{
			j=0;
			continue;
		} 
		int id=s_fin[i]-'A';
		while(j&&!trie[j][id]) j=f[j];
		j=trie[j][id];
		if(val[j]) cal(j);
		else if(last[j]) cal(last[j]);
	}
}

void getFail()
{
	queue<int> q;
	f[0]=0;
	for(int c=0;c<SIGMA_SIZE;c++)
	{
		int u=trie[0][c];
		if(u)	{f[u]=0;q.push(u);last[u]=0;} 
	}
	while(!q.empty())
	{
		int r=q.front();q.pop();
		for(int c=0;c<SIGMA_SIZE;c++)
		{
			int u=trie[r][c];
			if(!u)
			{
				trie[r][c]=trie[f[r]][c];
				continue;
			}
			q.push(u);
			int v=f[r];
			f[u]=trie[v][c];
			last[u]=val[f[u]]?f[u]:last[f[u]];
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值