• Uva1014: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
    dp[i]表示字符串从第i位往后有多少种分法
    转移方程: dp[i]=sum { dp[j+1] | s[i~j]为一个单词 }
    枚举每一个单词
    复杂度 每一组数据Θ(len(s)·S)
    略微有那么一点高

    想法二##

    还是dp,转移方程同上
    一个个枚举单词太慢了,我们发现对某一个i所有可以的单词前缀都应相同
    运用数据结构trie
    从s[i]开始在trie上找,找到某一个单词的结束就相当于找到了一个j
    复杂度 每一组数据Θ(maxlen(字典中的单词)·S)
    这样就可以过啦


    代码:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    
    using namespace std;
    
    const int N = 300005;
    const int P = 20071027;
    
    struct trie{
        trie *ch[26];
        int flag;
        void clear(){
            flag=0;
            for(int i=0;i<26;i++) ch[i]=NULL;     
        }
    }pool[105*4000],*root;
    int cnt;
    
    int d[N];
    void add(){
        char s[105];
        scanf("%s",s);
        int len=strlen(s),id;
        trie *p=root;
        for(int i=0;i<len;i++){
            id=s[i]-'a';
            if(!p->ch[id]){
                pool[++cnt].clear();
                p->ch[id]=&pool[cnt];  
            }
            p=p->ch[id];
        }
        p->flag++;
    }
    
    char sh[N];
    
    int main()
    {
        int len,n,i,j,kase=0;
        trie *p;
        root=&pool[++cnt];
        while(scanf("%s",sh)!=EOF){
            len=strlen(sh);
            scanf("%d",&n);
            pool[1].clear(); cnt=1;
            for(i=0;i<n;i++) add();
            d[len]=1;
            for(i=len-1;i>=0;i--){
                p=root;
                d[i]=0;
                for(j=i;j<len;j++){
                    if(p->ch[sh[j]-'a']) {
                        p=p->ch[sh[j]-'a'];
                        if(p->flag) 
                            d[i]=(d[i]+d[j+1])%P;                 
                    }
                    else break;
                }
            }
            printf("Case %d: %d
    ",++kase,d[0]);
        }
        
        return 0;    
    }
    
    既然选择了远方,便只顾风雨兼程
  • 相关阅读:
    cocos2d-android学习四 ---- 精灵的创建
    Think In java 笔记一
    管理文件夹
    Android Studio Mac 快捷键整理分享
    协同过滤
    POJ 3281(Dining-网络流拆点)[Template:网络流dinic]
    JS经常使用表单验证总结
    js中的Call与apply方法
    (转)WPF控件开源资源
    五年北京,这个改变我命运的城市,终于要离开了(转)
  • 原文地址:https://www.cnblogs.com/lindalee/p/8178850.html
Copyright © 2020-2023  润新知