码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive - 3942(trie

时间:2015-04-25 10:34:42      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

Description

技术分享

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

Trie上的DP,第一次写trie,发现不能写数组版的啊,比指针版的慢了好多倍。。。。。。。
const int maxv=4e5+3;
const ll mod=20071027;
char s[maxv];
int S;
int idx(char c){return c-a;}
struct trie{
    int ch[maxv][27];
    bool ma[maxv];
    int sz;
    void clear(){
        memset(ch,0,sizeof ch);
        memset(ma,0,sizeof ma);
        sz=1;
    }
    void insert(char *s){
        int n=strlen(s);
        int u=0,v;
        for(int i=0;i<n;i++){
            v=idx(s[i]);
            if(ch[u][v])
                u=ch[u][v];
            else{
                ch[u][v]=sz++;
                u=sz-1;
            }
        }
        ma[u]=1;
    }
}T;
char tmp[maxv];
const int maxdp=3e5+30;
ll dp[maxdp];
int main(){
    int t=0;
    while(scanf("%s",s)!=EOF){
        int ssz=strlen(s);
        t++;
        cin>>S;
        T.clear();
        memset(dp,0,sizeof dp);
        while(S--){
            scanf("%s",tmp);
            T.insert(tmp);
        }
        dp[ssz]=1;
        for(int i=ssz-1;i>=0;i--){////从最后一个字符开始匹配
            char *su=&s[i];
            int susz=strlen(su);
            int u=0;
            for(int j=0;j<susz;j++){
                int v=idx(su[j]);
                if(T.ch[u][v]&&T.ma[T.ch[u][v]]){
                        dp[i]=(dp[i+j+1]+dp[i])%mod;
                }
                if(T.ch[u][v]) u=T.ch[u][v];
                else break;
            }
        }
        printf("Case %d: %lld\n",t,dp[0]);
    }
    return 0;
}

 

UVALive - 3942(trie

标签:

原文地址:http://www.cnblogs.com/Cw-trip/p/4455421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!