Uva 1401 Remember the Word 字典树+DP

Neal is very curious about combinatorial problems, and now here comes a problem about words. Know-
ing 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


题意:给出一个字符串和一组单词,问用这些单词组成这个字符串有多少种不同的组合。

方法:字典树+DP

思路:首先考虑这个字符串,假设在这组单词中有一个是这个字符串的前缀,那么我们就可以不考虑这个前缀,直接考虑从前缀往后的字符串的组合方式。举个例子,对于abcd,单词中的a和ab都是它的前缀,假如是a,那么我们只需要知道bcd的组合方式即可;假如是ab,我们只需要知道cd的组合方式即可,也就是说这是一个逆推的DP,方程为dp[i]=sum{dp[i+len(x)|单词x是原始串的后缀S[i...L]的前缀},也就是说,我们必须从最后一个元素开始算,初始的时候,我们需要让dp[len]=1,因为假如一个前缀就是串本身,那么实际上只有一种组合方式。


#include <iostream>
#include <stdio.h>
#include <string.h>
#define MOD 20071027
using namespace std;

struct trie
{
	bool last;
	trie *next[26];
} ;

trie *root=new trie;
char word[300010];
char words[110];
int s;
int dp[300010];


void insert_trie()//向字典树中插入单词
{
	trie *p,*newnode;
	p=root;//容易忘,直接挂掉
	for(int i=0;words[i]!='\0';i++)
	{
		if(p->next[words[i]-'a']==NULL)
        {
            newnode=new trie;
            newnode->last=false;
            for(int j=0;j<26;j++)//注意新节点的初始化
                newnode->next[j]=NULL;
            p->next[words[i]-'a']=newnode;
            p=newnode;
        }
        else
        {
            p=p->next[words[i]-'a'];
        }
	}
	p->last=true;
}

void find_trie(int s)//查找串word
{
    trie *p=root;
    for(int i=s;word[i]!='\0';i++)
    {
        if(p->next[word[i]-'a']!=NULL)
        {
            if(p->next[word[i]-'a']->last==true)
            {
                dp[s]=(dp[s]+dp[i+1])%MOD;//居然忘记取模WA了。。。
            }
            p=p->next[word[i]-'a'];
        }
        else
        {
            return;
        }
    }
}

void getres()
{
    int len=strlen(word);
    dp[len]=1;//初始化
    for(int i=len-1;i>=0;i--)
    {
        find_trie(i);
    }
}

int main()
{
	int i,cas=0;
	while(~scanf("%s",word))
	{
	    memset(dp,0,sizeof(dp));
		for(i=0;i<26;i++)
			root->next[i]=NULL;
		scanf("%d",&s);
		for(i=0;i<s;i++)
		{
			scanf("%s",words);
			insert_trie();
		}
		getres();
		printf("Case %d: %d\n",++cas,dp[0]);
	}
    return 0;
}


你可能感兴趣的:(dp,word,uva,字典树,the,remember,1401)