UVA 3942 - Remember the Word (Trie)

10 篇文章 1 订阅
9 篇文章 0 订阅

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.


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值