现在的位置: 首页 > 综合 > 正文

LA 3942 – Remember the Word

2017年11月17日 ⁄ 综合 ⁄ 共 1919字 ⁄ 字号 评论关闭

Time limit: 3.000 seconds

Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie. 

Since Jiejie can't remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie's only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks. 

The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the
words in the set.

Input 

The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000. 

The second line contains an integer S ,
1$ \le$S$ \le$4000

Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase. 

There is a blank line between consecutive test cases. 

You should proceed to the end of file. 

Output 

For each test case, output the number, as described above, from the task description modulo 20071027. 

Sample Input 

abcd 
4 
a 
b 
cd 
ab

Sample Output 

 

Case 1: 2
 
Trie树。
 
 
#include<cstdio>
#include<cstring>

using namespace std;

const int MAXN=300111;

char S[MAXN];
int d[MAXN];

int ch[400111][26];
int value[400111];
int sz;

void word_insert(char *word)
{
	int i,u=1;
	int d=strlen(word);

	for(i=0;i<d;i++)
	{
		if(ch[u][word[i]-'a']==0)
		{
			ch[u][word[i]-'a']=++sz;
			memset(ch[sz],0,sizeof(ch[sz]));
			u=sz;
			value[u]=0;
		}
		else
		{
			u=ch[u][word[i]-'a'];
		}
	}
	value[u]=true;
}


int main()
{
	int n,i,j,len,u,tmp;
	int t=1;
	char word[111];
	while(scanf("%s",S)==1){
		scanf("%d",&n);

		sz=1;
		value[1]=false;
		memset(ch[1],0,sizeof(ch[0]));

		for(i=1;i<=n;i++){
			scanf("%s",word);
			word_insert(word);
		}
		getchar();

		len=strlen(S);
		memset(d,0,sizeof(d));
		d[0]=1;
		
		for(i=0;i<len;i++)
		{
			u=1;
			j=0;
			tmp=i;
			while(tmp<len && ch[u][S[tmp]-'a']!=0)
			{
				j++;
				if(value[ch[u][S[tmp]-'a']])
					d[i+j]=(d[i+j]+d[i])%20071027;
				u=ch[u][S[tmp]-'a'];
				tmp++;
				
			}
		}

		printf("Case %d: %d\n",t++,d[len]);
	}

	return 0;
}

 

抱歉!评论已关闭.