[POJ 1625] Censored! (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


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

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


本题坑点:

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

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

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


数据可以去POJ的discuss找。


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

#include 
#include 
#include 
#include 
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 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-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



你可能感兴趣的:(AC自动机,DP,大整数)