[POJ 1625] Censored! (AC自动机+DP+大整数)

5 篇文章 0 订阅
1 篇文章 0 订阅
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


很普通的一道禁止字符串的题,但是就是死活不AC,和网络上的一些程序也对拍过了大数据,都没问题。想不明白啊……先放着吧。

已AC,大整数输出写挂了←_←


本题坑点:

(1)据说输入数据的字符中会有空格,所以请不要用scanf加%s读入,用gets。

(2)据说输入数据的字符可能会大于127(为负数),所以请不要用数组来映射,建议用map。

(3)这题空间有比较严的限制,所以10进制的大整数是行不通的。


数据可以去POJ的discuss找。


以下是一段蠢得不行的代码。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
using namespace std;
const int bignlen = 60, MOD = 1000000, perlen = 6;

struct trnode
{
	char chr;
	int nxt[55], fail, last, val;
};

struct bign
{
	bign(){memset(bn, 0, sizeof(bn));}
	int bn[bignlen];
};

void addstr(char*);
void calfail();
void bignprint(bign);

bign operator + (bign a, bign b)
{
	bign tem;
	for(int i=0; i<bignlen-1; i++)
	{
		tem.bn[i] += a.bn[i] + b.bn[i];
		tem.bn[i+1] += tem.bn[i]/MOD;
		tem.bn[i]%=MOD;
	}
	return tem;
};

int N,M,P;
int chn[1000];
char alp[1000];
trnode trie[550];
int triep;
int que[505];
bign dp[51][550];
bool vis[51][550];
map<char, int> mp;

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	while(scanf("%d%d%d\n", &N, &M, &P ) != EOF)
	{
		triep = 0;
		memset(trie, 0, sizeof(trie));
		memset(chn, 0, sizeof(chn));
		memset(dp, 0, sizeof(dp));
		memset(vis, 0, sizeof(vis));
		mp.clear();
		gets(alp);
		for(int i=0; i<N; i++) mp[alp[i]] = i;//chn[alp[i]] = i;
		for(int i=0; i<P; i++)
		{
			char patn[20];
			gets(patn);
			addstr(patn);
		}
		calfail();

		dp[0][0].bn[0] = 1;
		vis[0][0] = 1;
		for(int i=0; i<M; i++)
		{
			for(int np=0; np<=triep; np++)
			{
				if(!vis[i][np]) continue;
				for(int k=0; k<N; k++)
				{
					int nxp = trie[np].nxt[k];
					if(trie[nxp].val) continue;
					dp[i+1][nxp] = dp[i+1][nxp] + dp[i][np];
					vis[i+1][nxp] = 1;
				}
			}
		}

		bign ans;
		for(int i=0; i<=triep; i++)
			ans = ans + dp[M][i];

		bignprint(ans);
	}
	return 0;
}

void bignprint(bign x)
{
	int len = bignlen-1;
 	while(!x.bn[len] && len>-1) len--;
	if(len == -1)
		printf("0");
	else
	{
		for(int i=len; i>-1; i--)
		{
			int tem = x.bn[i];
			if(i != len)
			{
				int ilen = 0;
				while(tem) tem/=10, ilen++;
				for(int j=1; j<=perlen-ilen; j++) printf("0");
			}
			if(x.bn[i]) printf("%d", x.bn[i]); //原来这里没有if,导致这一位全部为0的话,会多输出一个0
		}
	}
	puts("");
	return;
}

void addstr(char *patn)
{
	int len = strlen(patn);
	int np = 0;
	for(int i=0; i<len; i++)
	{
		int chnum = mp[patn[i]];//chn[patn[i]];
		int nxp = trie[np].nxt[chnum];
		if(nxp)
			np = nxp;
		else
		{
			triep++;
			trie[np].nxt[chnum] = triep;
			np = triep;
		}
		if(i==len-1) trie[np].val = 1;
	}
	return;
}

void calfail()
{
	int qhead = 0, qtail = 0;
	for(int i=0; i<N; i++)
		if(trie[0].nxt[i])
			que[++qtail] = trie[0].nxt[i];
			
	while(qhead<qtail)
	{
		int np = que[++qhead];
		for(int i=0; i<N; i++)
		{
			int nxp = trie[np].nxt[i];
			int fap = trie[np].fail;
			if(!nxp){ trie[np].nxt[i] = trie[fap].nxt[i]; continue;}
			que[++qtail] = nxp;
			trie[nxp].fail = trie[fap].nxt[i];
			fap = trie[nxp].fail;
			if(trie[fap].val) trie[nxp].val = 1;
		}
	}
	return;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值