UVA 3942 - Remember the Word (Trie)

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,
1S4000.

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.

SampleInput

abcd
4
a
b
cd
ab

SampleOutput

Case 1: 2

解析:不难想到这样的递推法:令d(i)表示从字符i开始的字符串(即后缀S[i...L])的分解方案数,则d(i) = sum{d(i + len(x))|单词x是S[i...L]的前缀}。

如果先枚举x,再判断它是否为S[i...L]的前缀,时间无法承受(最多可能有4000个单词,判断还需要一定的时间)。可以换一个思路,先把所有的单词组织成Trie,然后试着在Trie中“查找”S[i...L]。查找过程中每经过一个单词结点,就找到一个上述状态转移方程中的x,最多只需要比较100次就能找到所有的x。

AC代码:

#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;

const int maxnode = 4000 * 100 + 10;
const int sigma_size = 26;

const int MOD = 20071027;
const int maxl = 300000 + 10;
const int maxw = 4000 + 10;
const int maxwl = 100 + 10;

struct Trie{
    int ch[maxnode][sigma_size];    //ch[i][j]表示结点i的编号为j的子结点,int类型!!!
    int val[maxnode];
    int sz;

    void clear(){ sz = 1; memset(ch[0], 0, sizeof(ch[0])); }

    int idx(char c){ return c - 'a'; }

    void insert(const char *s, int v){
        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] = v;
    }

    void find_prefixes(const char *s, int len, vector<int>& ans){
        int u = 0;
        for(int i = 0; i < len; i ++){
            if(s[i] == '\0') break;
            int c = idx(s[i]);
            if(!ch[u][c]) break;
            u = ch[u][c];
            if(val[u] != 0) ans.push_back(val[u]);
        }
    }
};

int d[maxl], len[maxw], cnt_word;
char text[maxl], word[maxwl];
Trie trie;
vector<int> ans;

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    int kase = 0;
    while(scanf("%s%d", text, &cnt_word) == 2){
        trie.clear();
        for(int i = 1; i <= cnt_word; i ++){
            scanf("%s", word);
            len[i] = strlen(word);
            trie.insert(word, i);
        }
        memset(d, 0, sizeof(d));
        int L = strlen(text);
        d[L] = 1;
        for(int i = L-1; i >= 0; i --){
            ans.clear();
            trie.find_prefixes(text+i, L-i, ans);
            for(int j = 0; j < ans.size(); j ++)
                d[i] = (d[i] + d[i+len[ans[j]]]) % MOD;    //状态转移方程
        }
        printf("Case %d: %d\n", ++kase,  d[0]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-01 22:43:56

UVA 3942 - Remember the Word (Trie)的相关文章

LA ——3942 - Remember the Word(Trie 入门)

3942 - Remember the Word Regionals 2007 >> Asia - Nanjing Time limit: 3.000 seconds ------------------------------------------------------ 从右往左地推,令dp[i] 表示字符串  S[i....len]的分解方案数,则dp[i]=sum(dp[i+len(x)])  ,我们只要枚举 S[i....len]的前缀,在所给的单词中查找前缀,如果存在,则进行状态

UVALive - 3942 - Remember the Word (Trie树)

UVALive - 3942 Remember the Word Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a p

LA3942-Remember the Word(Trie)

题意: 有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法 分析: dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len(x)]);单词x是i开始的字符串的前缀. #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #

LA 3942 Remember the Word (Trie树)

——刘汝佳的白皮书里面介绍的题目. /* Problem: Status : By WF, */ #include "algorithm" #include "iostream" #include "cstring" #include "cstdio" #include "string" #include "stack" #include "cmath" #inclu

UVA - 1401 Remember the Word(trie+dp)

1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树,然后从后向前dp,dp[i]=dp[i]+dp[i+len(x)]; 其中x为当前找到的前缀长度. 3. #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #defin

UVALive 3942 - Remember the Word(DP,数组Trie+指针Trie)

UVALive 3942 - Remember the Word(DP,数组Trie+指针Trie) ACM 题目地址: UVALive 3942 - Remember the Word 题意: 给一些单词,然后给一个长的单词,问有几种方法能组成这个大单词,单词可以重复用. 分析: DP[i]=sum{DP[j} (i<j<len),从后往前求. 本来用数组Trie写得爽爽的,1A了. 发现2s多,不能忍! 然后用指针Trie写了一遍,各种出错,整个人都不好了... 研究了一遍别人代码,发现快

Uva 10815-Andy&#39;s First Dictionary(串)

Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

UVA 10375 Choose and divide(数论)

The binomial coefficient C(m,n) is defined as m! C(m,n) = -------- n!(m-n)! Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s). The Input Input consists of a sequence of lines. Each line contains four non-n