zoukankan      html  css  js  c++  java
  • UVALive

    Description

    Download as PDF

    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
    
    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
    ",t,dp[0]);
        }
        return 0;
    }
  • 相关阅读:
    Java 基础 泛型
    Hibernate 注解
    Head Fisrt Android Development读书笔记(7)Database Persistent
    ruby中的Enumerable的使用
    [置顶] 从small到safe,形容词的学问
    Silverlight中DomainDataSource的一种Debug方法
    [置顶] 从高中一次半夜不冲厕所的经历谈程序
    [置顶] 视频网站:一炷香后即将为您播放精彩内容
    Android. Handling some SQLite issues
    查找ruby方法(以rails为例)
  • 原文地址:https://www.cnblogs.com/Cw-trip/p/4455421.html
Copyright © 2011-2022 走看看