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;
    }
    


  • 相关阅读:
    织梦后台如何生成站点地图sitemap.xml
    滚轮滚动一定距离触发导航顶部固定,且所点击的关键词,背景字体变色
    超简单横向选项卡
    后台域名重定向301
    JS检测是否是360浏览器
    《深入理解Java虚拟机》-----第2章 Java内存区域与内存溢出异常
    java基础(十七)----- 浅谈Java中的深拷贝和浅拷贝 —— 面试必问
    java基础(十六)----- equals()与hashCode()方法详解 —— 面试必问
    java基础(十五)----- Java 最全异常详解 ——Java高级开发必须懂的
    java基础(十四)-----详解匿名内部类——Java高级开发必须懂的
  • 原文地址:https://www.cnblogs.com/pangblog/p/3315235.html
Copyright © 2011-2022 走看看