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<map>      
#include<stack>      
#include<queue>      
#include<vector>      
#include<math.h>      
#include<stdio.h>      
#include<iostream>      
#include<string.h>      
#include<stdlib.h>      
#include<algorithm>      
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];
queue<int>q;
void change()
{
	int len=strlen(ss),i;
	for(i=0;i<len;i++)
		hashs[ss[i]]=++cnt;
}
void insert(int id)
{
    int i,len=strlen(s1),now=0,cnt=0;
    for(i=0;i<len;i++)
    {
        int v=hashs[s1[i]];
        if(!a[now][v])
		{
			flag[size]=0;
			memset(a[size],0,sizeof(a[size]));
            a[now][v]=size++;
		}
		now=a[now][v];
    }
	flag[now]=1;
}
void build_fail()
{
    int now,i;
	for(i=1;i<=cnt;i++)
	{
		int tmp=a[0][i];
		if(tmp)
			pre[tmp]=0,q.push(tmp);
	}
	while(q.empty()==0)
    {
		now=q.front();
		q.pop();
		if(flag[pre[now]])
			flag[now]=1;
        for(i=1;i<=cnt;i++)
        {
            if(a[now][i]==0)
			{
				a[now][i]=a[pre[now]][i];
                continue;
			}
			pre[a[now][i]]=a[pre[now]][i];
			q.push(a[now][i]);
        }   
    }
}  
void add(ll *a,ll *b)//大数相加
{
    int i,c=0;
    for(i=0;i<30;i++)
    {
        a[i]=a[i]+b[i]+c;
        c=a[i]/10000;
        a[i]=a[i]%10000;
    }
}
void work()
{
	int i,j,k;ll ans[50];
	memset(ans,0,sizeof(ans));
	memset(dp,0,sizeof(dp));
	dp[0][0][0]=1;
	for(i=0;i<m;i++)
		for(j=0;j<size;j++)
		{
			if(flag[j])continue;
			for(k=1;k<=cnt;k++)
			{
				int v=a[j][k];
				if(flag[v])continue;
				add(dp[i+1][v],dp[i][j]);
			}
		}
	for(i=0;i<size;i++)
		if(flag[i]==0)
			add(ans,dp[m][i]); 
	for(i=30;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<p;i++)
		{        
			scanf("%s",s1);
			insert(i);
		}
		build_fail();
		work();
		while(q.empty()==0)
			q.pop();
	}
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值