zoukankan      html  css  js  c++  java
  • LA 3942 Remember the Word(前缀树&树上DP)

                                                                                   

    3942 - 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$ 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
    

    题意:

    该题为大白(刘汝佳。入门经典训练指南)上一道例题。p209。

    思路:

    dp[i]=sum(dp[i+len(x)])

    dp[i]表示从字符i开始的字符串即后缀(s[i..L])的分解方案数。

    x为是(s[i..L]的前缀。

    详细见代码:

    #include <iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    const int md=400110;//单词数乘上单词长度。顶多一个单词一条路径
    const int ssz=26;
    const int maxn=300010;
    const int mod=20071027;
    char words[maxn];
    int dp[maxn];
    
    struct Trie
    {
        int ch[md][ssz];
        int val[md];
        int sz;
        void init()
        {
            sz=1;
            val[0]=0;
            memset(ch[0],0,sizeof ch[0]);
        }
        int idx(char c)
        {
            return c-'a';
        }
        void inser(char *s)
        {
            int u=0,n=strlen(s);
            for(int i=0; i<n; i++)
            {
                int c=idx(s[i]);
                if(!ch[u][c])
                {
                    memset(ch[sz],0,sizeof ch[sz]);
                    val[sz]=0;
                    ch[u][c]=sz++;
                }
                u=ch[u][c];
            }
            val[u]=n;//可以传参数初始化
        }
        void sear(char *s,int p,int len)
        {
            int u=0;
            for(int i=0; i<len; i++)//字符一个一个查找
            {
                int c=idx(s[i]);
                if(!ch[u][c])//前缀搜完
                    return ;
                u=ch[u][c];
                if(val[u])
                    dp[p]=(dp[p]+dp[p+val[u]])%mod;
            }
        }
    } tree;
    int main()
    {
        int i,s,len,cas=1;
        char tmp[110];
    
        while(~scanf("%s",words))
        {
            scanf("%d",&s);
            len=strlen(words);
            tree.init();//开始忘了初始化。调了半天。。。。。
            dp[len]=1;//注意这个位置的初始化!
            for(i=0; i<s; i++)
            {
                scanf("%s",tmp);
                tree.inser(tmp);
            }
            for(i=len-1; i>=0; i--)
            {
                dp[i]=0;
                tree.sear(words+i,i,len-i);
            }
            printf("Case %d: %d
    ",cas++,dp[0]);
        }
        return 0;
    }
    


  • 相关阅读:
    Eq Eqv Equal
    list append 总是复制前面的参数,而不复制最后一个参数
    Teach Yourself Scheme in Fixnum Days 13 Jump跳转
    python 操作 office
    python ImportError: DLL load failed: %1 不是有效的 Win32 应用程序
    pywin32 安装错误 ImportError: DLL load failed: 不是有效的 Win32 应用程序
    Python version 2.7 required, which was not found in the registry
    scheme 解释器Guile 使用
    Teach Yourself Scheme in Fixnum Days 6 recursion递归
    求一个正则表达式,字母不能重复
  • 原文地址:https://www.cnblogs.com/pangblog/p/3315235.html
Copyright © 2011-2022 走看看