LA 3942 - Remember the Word 字典树 dp

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

------------------------------------------------------------
给出一个由S个不同单词组成的字典和一个长字符串,把这个字符串分解成若干个单词的链接,单词可以重复使用,有多少种方法?
f[i]表示以字符i为结尾的单词分解方法,若一个长度为x的单词与以i+1为开头的部分相匹配,则f[i+x]=f[i+x]+f[i];
将单词建立一个字典树,用字典树快速求出匹配的单词。


------------------------------------------------------------
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>

using namespace std;

char str[333333];
char words[4444][111];
int f[333333];

//用类,或者结构体定义都行
struct trie
{
    trie* next[26];
    int num;
    int value;
    trie()
    {
       for(int i=0;i<26;i++) next[i]=NULL;
       value=0;//记录是不是一个单词
       num=0;//记录单词出现的次数
    }
    void clear()
    {
       for(int i=0;i<26;i++) next[i]=NULL;
       value=0;//记录是不是一个单词
       num=0;//记录单词出现的次数
    }
}root;

//插入:
void insert(char* s)
{
    trie* p=&root;
    int k=0;
    while(s[k]!='\0')
    {
        if(!p->next[s[k]-'a']) p->next[s[k]-'a']=new trie;
        p=p->next[s[k]-'a'];
        p->num++;
        k++;
    }
    p->value=1;
}

//查找
void find(char* s,int pos)
{
    trie* p=&root;
    int k=0;
    while(s[k]!='\0'&&p->next[s[k]-'a'])
    {
        p=p->next[s[k]-'a'];
        if (p->value==1)
        {
            f[pos+k+1]=(f[pos+k+1]+f[pos])%20071027;
        }
        k++;
    }
}

int main()
{
    int l,s;
    int cnt=0;
    while (~scanf("%s",str+1))
    {
        cnt++;
        memset(f,0,sizeof(f));
        root.clear();
        scanf("%d",&s);
        l=strlen(str+1);
        for (int i=0;i<s;i++)
        {
            scanf("%s",words[i]);
            insert(words[i]);
        }
        f[0]=1;
        for (int i=1;i<=l;i++)
        {
            if (f[i-1])
            {
                find(str+i,i-1);
            }
        }
        printf("Case %d: %d\n",cnt,f[l]);
    }
    return 0;
}








posted on 2013-04-19 19:49  电子幼体  阅读(218)  评论(0编辑  收藏  举报

导航