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


  • 相关阅读:
    Oracle——oem开启managementserver的时候搜索不到节点的解决方法!!!
    PL/SQL学习笔记(一)
    oracle 数据库的非指令备份方法
    OracleDBConsole[SID]服务简介
    Toad 使用快速入门
    oracle 配置
    sql中exist与in 的区别
    页面之间传递参数的几种方法荟萃
    div+CSS网页布局入门系列教程(来自标准之路)
    XML操作类
  • 原文地址:https://www.cnblogs.com/pangblog/p/3315235.html
Copyright © 2011-2022 走看看