POJ1625--AC自动机+DP+大数

Description

The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters without word breaks. So, there exist exactly N^M different Freish sentences. 

But after recent election of Mr. Grass Jr. as Freeland president some words offending him were declared unprintable and all sentences containing at least one of them were forbidden. The sentence S contains a word W if W is a substring of S i.e. exists such k >= 1 that S[k] = W[1], S[k+1] = W[2], ...,S[k+len(W)-1] = W[len(W)], where k+len(W)-1 <= M and len(W) denotes length of W. Everyone who uses a forbidden sentence is to be put to jail for 10 years. 

Find out how many different sentences can be used now by freelanders without risk to be put to jail for using it. 

Input

The first line of the input file contains three integer numbers: N -- the number of letters in Freish alphabet, M -- the length of all Freish sentences and P -- the number of forbidden words (1 <= N <= 50, 1 <= M <= 50, 0 <= P <= 10). 

The second line contains exactly N different characters -- the letters of the Freish alphabet (all with ASCII code greater than 32). 

The following P lines contain forbidden words, each not longer than min(M, 10) characters, all containing only letters of Freish alphabet. 

Output

Output the only integer number -- the number of different sentences freelanders can safely use.

Sample Input

2 3 1
ab
bb

Sample Output

5

题意:给出包含n个可见字符的字符集,以下所提字符串均由该字符集中的字符构成。给出p个长度不超过10的字符串,求长为m且不包含上述p个字符串的字符串有多少个。

思路:将所给的病毒串构建AC自动机。dp[i][j]表示长度为i,状态为j的个数。对每一个状态枚举往后添加的字符。

有dp[i+1][转移到的状态] += dp[i][j];

需要用到大数相加。

#include 
map  coll;
#define maxn 58
BigNum dp[maxn][112];
int Id[400];
int first,rear,Cnt;
struct node
{
	node * fail;
	node * next[maxn];
	int end,cnt;
	node()
	{
		end = 0;
		cnt = Cnt++;
		for(int i = 0;i < maxn;i++)
		{
			next[i] = NULL;
		}
	}
}*q[112],*qq[112];

void insert(char * s,node * root)
{
	node * p = root;
	int len = strlen(s);
	for(int i = 0;i < len;i++)
	{
		int id = coll[s[i]];
		if(p -> next[id] == NULL)
			p -> next[id] = qq[Cnt-1] = new node();
		p = p -> next[id];
	}
	p -> end = 1;
}

void build_ac_automation(node * root)
{
	q[rear++] = root;
	node * p = NULL;
	while(first < rear)
	{
		p = q[first++];
		for(int i = 0;i < maxn;i++)
		{
			if(p -> next[i] != NULL)
			{
				if(p == root)
					p -> next[i] -> fail = root;
				else
				{
					p -> next[i] -> fail = p -> fail -> next[i];
					if(p -> fail -> next[i] -> end)
						p -> next[i] -> end = 1;
				}
				q[rear++] = p -> next[i];
			}
			else
			{
				if(p == root)	 p -> next[i] = root;
				else p -> next[i] = p -> fail -> next[i];
			}
		}
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	int n,m,p;
	while(scanf("%d%d%d",&n,&m,&p)==3)
	{
		first = rear = Cnt = 0;
		node * root = qq[0] = new node();
		char str[maxn];
		scanf("%s",str);
		int len = strlen(str);
		for(int i = 0;i < len;i++)
			coll[str[i]] = i;
		while(p--)
		{
			scanf("%s",str);
			insert(str,root);
		}
		build_ac_automation(root);
		for(int i = 0;i <= m;i++)
			for(int j = 0;j < Cnt;j++)
				dp[i][j] = 0;
		dp[0][0] = 1;
		for(int i = 0;i < m;i++)
			for(int j = 0;j < Cnt;j++)
			{
				if(qq[j] -> end)	continue;
				for(int k = 0;k < len;k++)
				{
					if(qq[j] -> next[k] -> end)	continue;
					dp[i+1][qq[j]->next[k]->cnt] = dp[i][j] + dp[i+1][qq[j]->next[k]->cnt];
				}
			}
		BigNum ans = 0;
		for(int i = 0;i < Cnt;i++)	
			ans = ans + dp[m][i];
		ans.print();
	}
	return 0;
}



你可能感兴趣的:(字符串_AC自动机)