ac自动机(dp)<AC自动机模板> ---高精度处理

#include #include #include #include #include #include #include #include //poj1625 ac自动机(dp)---高精度处理(不能用矩阵乘法)using namespace std;int n, m, k, vis[200], dp[60][110][100]; //dp数组第一维表示单词长度,第
摘要由CSDN通过智能技术生成
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string.h>
#include <queue>
#include <vector>   //poj1625  ac自动机(dp)---高精度处理(不能用矩阵乘法)
using namespace std;
int n, m, k, vis[200], dp[60][110][100];  //dp数组第一维表示单词长度,第二维表示到达什么节点(位置),第三维表示路径数目(用大数表示)

struct node
{
	int isword;
	int fail;
	int next[50];
	void init()
	{
		for(int t=0; t<50; ++t)
			next[t]=0;
		isword=0;
		fail=-1;
	}
}a[110];

void insert(char *p)
{
	int t, h, g=0, j=strlen(p);
	for(t=0; t<j; ++t)
	{
		h=vis[p[t]-32];
		if(!a[g].next[h])
		{
			a[k].init();
			a[g].next[h]=k++;
		}
		g=a[g].next[h];
	}
	a[g].isword=1;
	return ;
}

void acAutomation()    //优化过的,可以当模板
{
	int t, temp;
	queue<int> q;
	q.push(0);
	while(!q.empty())
	{
		int s=q.front();
		q.pop();
		for(t=0; t<n; ++t)
		{
			if(a[s].next[t]==0)
			{
				if(s==0)a[s].next[t]=0;
				else a[s].next[t]=a[a[s].fail].next[t];  //注意这里
			}
			else 
			{
				temp=a[s].fail;
				if(s==0)
				{
					a[a[s].next[t]].fail=0;
				}
				else 
				{
					while(temp!=-1)
					{
						if(a[temp].next[t])
						{
							a[a[s].next[t]].fail=a[temp].next[t];
							break;
						}
						temp=a[temp].fail;
					}
					if(temp==-1)
						a[a[s].next[t]].fail=0;
				}
				a[a[s].next[t]].isword|=a[a[a[s].next[t]].fail].isword;
				q.push(a[s].next[t]);
			}
		}
	}
}
void add(int *a1, int *b1)  //大数加法
{
	int t, c=0;
	for(t=0; t<100; ++t)
	{
		a1[t]=a1[t]+b1[t]+c;
		c=a1[t]/10;
		a1[t]%=10;
	}
	return ;
}

void solve()
{
	int i, t, j, g;
	memset(dp, 0, sizeof(dp));
	dp[0][0][0]=1;
	for(i=1; i<=m; ++i)   //长度,类似矩阵一样(长度为m)就要m遍相加(矩阵就要m遍相乘)
	{
		for(j=0; j<k; ++j)
		{
			if(a[j].isword)
				continue;
			for(t=0; t<n; ++t)
			{
				g=a[j].next[t];
				if(a[g].isword)
					continue;
				add(dp[i][g], dp[i-1][j]);   //dp状态转移dp[i][g]+=dp[i-1][j]只不过这里要对大数处理,所以用第三维来储存大数的没一个数字
			}
		}
	}
	int ans[100];
	memset(ans, 0, sizeof(ans));
	for(t=0; t<k; ++t)   //0包括哪些没有在字典树上的点(同样也有相同的长度m)的种类数
	{
		if(!a[t].isword)
			add(ans, dp[m][t]);
	}
	for(t=99; t>=0; --t)
	{
		if(ans[t])break;
	}
	if(t<0)
		printf("0\n");
	else 
	{
		while(t>=0)
		{
			printf("%d", ans[t]);
			t--;
		}
		printf("\n");
	}
	return ;
}

int main()
{
	int d, t, j, g;
	char p[60];
	while(scanf("%d%d%d", &n, &m, &d)!=EOF)
	{
		a[0].init();
		k=1;
		scanf("%s", p);
		j=strlen(p);
		for(t=g=0; t<j; ++t)
		{
			vis[p[t]-32]=g++;   //vis表示不同字母的序号
		}
		for(t=0; t<d; ++t)
		{
			scanf("%s", p);
			insert(p);
		}
		acAutomation();
		solve();
	}
	return 0;
}

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值