POJ 1625(ac自动机+dp+高精度)

大牛:http://www.cnblogs.com/kuangbin/archive/2013/06/27/3159954.html

Censored!

Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 6956 Accepted: 1887

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
 
 
很好的题目。。。
 
注意字符貌似会有超过128的,用map转化下就好了。
 
然后就是用AC自动机实现状态表。
然后高精度 进行简单的DP;

AC自动机要记录存在节点的fail指针,也要记录存在的节点的不存在的儿子的fai指针;
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
int max(int x,int y){
	return x>y?x:y;
}
struct Matrix{
    int mat[110][110];
    int n;
    Matrix(){}
    Matrix(int _n)
    {
        n=_n;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                mat[i][j] = 0;
    }
};
struct Tree{
	int next[55];
	int end,fail;
	void clear(){
		for(int i=0;i<55;i++)
			next[i]=0;
		end=fail=0;
	}
}tree[200];
struct BigInt{
	const static int mod = 10000;
    const static int DLEN = 4;
	int a[100],len;
	BigInt()
    {
        memset(a,0,sizeof(a));
        len = 1;
    }
	BigInt(int v){
        memset(a,0,sizeof(a));
        len=0;
        do{
            a[len++]=v%mod;
            v/=mod;
        }while(v);
    }
	BigInt operator +(const BigInt &b)const
    {
        BigInt res;
        res.len=max(len,b.len);
        for(int i=0;i<=res.len;i++)
            res.a[i]=0;
        for(int i=0;i<res.len;i++)
        {
            res.a[i]+=((i<len)?a[i]:0)+((i<b.len)?b.a[i]:0);
            res.a[i+1]+=res.a[i]/mod;
            res.a[i]%=mod;
        }
        if(res.a[res.len]>0)res.len++;
        return res;
    }
    BigInt operator *(const BigInt &b)const
    {
        BigInt res;
        for(int i=0;i<len;i++)
        {
            int up=0;
            for(int j=0;j<b.len;j++)
            {
                int temp=a[i]*b.a[j]+res.a[i+j]+up;
                res.a[i+j]=temp%mod;
                up=temp/mod;
            }
            if(up!=0)
                res.a[i+b.len]=up;
        }
        res.len=len+b.len;
        while(res.a[res.len-1]==0&&res.len>1)res.len--;
        return res;
    }
    void output()
    {
        printf("%d",a[len-1]);
        for(int i=len-2;i>=0;i--)
            printf("%04d",a[i]);
        printf("\n");
    }
};
map<char,int>mp;
char str[100];
int tot,f[200][200],qq,n,m,p;
BigInt dp[2][200];
void tree_insert(){
	int root,l,t;
	l=strlen(str);
	t=0;root=0;
	while(t<l){
		//printf("%d\n",mp[str[t]]);
		if(tree[root].next[mp[str[t]]]!=0)root=tree[root].next[mp[str[t]]];
		else{
			tot++;
			tree[tot].clear();
			tree[root].next[mp[str[t]]]=tot;
			root=tot;
			
		}
		t++;
	}
	tree[root].end=1;
	return ;
}
void ac_build(){
	queue<int>Q;
	tree[0].fail=0;
	for(int i=0;i<n;i++)
		if(tree[0].next[i]==0)tree[0].next[i]=0;
		else {tree[tree[0].next[i]].fail=0;Q.push(tree[0].next[i]);}
	while(!Q.empty()){
		int s=Q.front();
		Q.pop();
		//printf("%d\n",s);
		//for(int i=0;i<n;i++)printf("%d ",tree[s].next[i]);printf("\n");
		if(tree[tree[s].fail].end==1)tree[s].end=1;
		for(int i=0;i<n;i++)
			if(tree[s].next[i]==0){tree[s].next[i]=tree[tree[s].fail].next[i];
			//if(s==3)printf("%d %d\n",i,tree[tree[s].fail].next[i]);
			}
			else {tree[tree[s].next[i]].fail=tree[tree[s].fail].next[i];Q.push(tree[s].next[i]);}
	}
	return ;
	
}
void get(){
	int i,j;
	for(i=0;i<=tot;i++)
		for(j=0;j<=tot;j++)
			f[i][j]=0;
	for(i=0;i<=tot;i++)
		for(j=0;j<n;j++)
			if(tree[tree[i].next[j]].end==0){
				f[i][tree[i].next[j]]++;//printf("%d %d\n",i, tree[i].next[j]);
				}
	
	return ;
}
int main(){
	int i;
	char ch;
	while(scanf("%d%d%d",&n,&m,&p)!=EOF){
		gets(str);
		mp.clear();
		for(i=0;i<n;i++){
			scanf("%c",&ch);
			mp[ch]=i;
		}
		gets(str);
		tot=0;
		tree[tot].clear();
		
		for(i=1;i<=p;i++){
			gets(str);
			tree_insert();
		}
		ac_build();
		qq=0;
		get();
		//printf("%d\n",tree[0].end);
		int now=0;
        dp[now][0]=1;
        for(int i=1;i<=tot;i++)dp[now][i]=0;
        //dp[0][0].output();
        for(int i=0;i<m;i++){
            now^=1;
            for(int j=0;j<=tot;j++)dp[now][j]=0;
            for(int j=0;j<=tot;j++)
                for(int k=0;k<=tot;k++)
                    if(f[j][k]>0)
                        dp[now][k]=dp[now][k]+dp[now^1][j]*f[j][k];
        }
        BigInt ans=0;
        for(int i=0;i<=tot;i++)//dp[now][i].output();
            ans=ans+dp[now][i];
        ans.output();
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值