POJ 1625-Censored!(AC自动机+DP+大数加法)

Censored!
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 10258   Accepted: 2805

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

Source

Northeastern Europe 2001, Northern Subregion

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题意:给你n个合法字符,p个非法序列,问你能造多少种长为m的合法序列。。。。。。

题解:一道挺水的DP,dp[i][j]:前i位且匹配到状态j的方案数。。挺好写的,然而wa了一晚上,第二天早上才发现可能会爆long long,好吧好吧,大整数加法走起。。。。

#include      
#include      
#include      
#include      
#include      
#include      
#include      
#include      
#include      
#include      
using namespace std;      
typedef long long  ll;      
#define inf 1000000000     
#define mod 20090717       
#define  maxn  16500    
#define  lowbit(x) (x&-x)      
#define  eps 1e-10  
int pre[maxn],a[maxn][300],flag[maxn],size,n,m,p,cnt,hashs[300];
ll dp[55][110][30];
char s1[30],ss[60];
queueq;
void change()
{
	int len=strlen(ss),i;
	for(i=0;i=0;i--)
		if(ans[i]!=0)
			break;
	if(i<0)
		printf("0");
	else
	{
		printf("%d",ans[i]);
		i--;
		for(;i>=0;i--)
			printf("%04d",ans[i]);
	}
	printf("\n");
}
int  main(void)
{
    int i;
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)
	{
		size=1;flag[0]=0;cnt=0;
		memset(pre,0,sizeof(pre));
		memset(a[0],0,sizeof(a[0]));
		memset(hashs,0,sizeof(hashs));
		scanf("%s",ss);
		change();
		for(i=0;i


你可能感兴趣的:(AC自动机)