UVaLive3942 Remember the Word (dp+字典树)

UVaLive3942 Remember the Word

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 ≤ S ≤ 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

题解

题意

给S个不同的单词和一个长字符串,问将其分解为若干个单词有多少种方法(单词可重复使用)

思路

设dp[i]表示以i开头的字符串分解的方法数。状态转移方程:dp[i]=sum(dp[i+len(x)]),x为S[i……L]的前缀。

将每一个单词插入到字典树中,然后考虑长字符串S的每一个后缀s[i……L], 看其前缀x是否在字典树中出现,每出现一次,dp[i]累加dp[i+len(x)]即可。

注意dp的初始化。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int MAXN = 4e5+10;
int n;
 
const int MOD = 20071027;
int dp[MAXN];
char s[MAXN];
const int SIZE =64;

struct Trie
{
	int sz,val[MAXN];int ch[MAXN][SIZE];
	
	void init(){
		sz = 1;
		memset(val,0,sizeof(val));
		memset(dp,0,sizeof(dp));
		memset(ch[0],0,sizeof(ch[0]));
	}
 
	int idx(char a){
		return a-'a'+1;	
	}
	
	void insert(char *s, int v){
		int u = 0,n = strlen(s);
		for(int i=0;i<n;i++){
			int c = idx(s[i]);
			if(!ch[u][c]){
				memset(ch[sz],0,sizeof(ch[sz]));
				val[sz] = 0;
				ch[u][c] = sz++;
			}
			u = ch[u][c];
		}
		val[u]=v;
	}
	int query(char *s){
		int u =0,n = strlen(s);
		for(int i=0;i<n;i++){
			int c = idx(s[i]);
			if(!ch[u][c]){
				return -1;
			}
			u = ch[u][c];
		}
		return val[u];
	}
	
	void update(int len,int pos){
		int u=0;
    	for(int i=pos; i<len; ++i){
        	int c=idx(s[i]);
        	if(!ch[u][c]) return;
        		u=ch[u][c];
        	if(val[u]==1) dp[pos]=(dp[pos]+dp[i+1])%MOD;
    	}
	}
 	
};
Trie tree;
int main(){
	
	int kase=0;
	while(~scanf("%s",s)){
		int N= 0;
		kase++;
		tree.init();
		scanf("%d",&N);
		for(int i=0;i<N;i++){
			char cur[MAXN];
			scanf("%s",cur);
			tree.insert(cur,1);
		}
		int len = strlen(s);
		dp[len]=1;
		for(int i=len-1;i>=0;i--){
			tree.update(len,i);
		}
		printf("Case %d: %d\n",kase,dp[0]);
	}
}
posted @ 2018-08-11 17:02  caomp  阅读(140)  评论(0)    收藏  举报