[Trie树][dp] LA3942 Remember the Word

31 篇文章 0 订阅
11 篇文章 0 订阅

@(ACM题目)[字符串, Trie树]

Description

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 alculate 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.

Sample Input

abcd
4
a
b
cd
ab

Sample Output

Case 1: 2

题目分析

本次给定一字符串 s ,以及一个字典(字符串的set),问将字符串拆成字典中的单词(不能相交)有多少种拆法。
本题属于基础的Trie树,利用Trie树优化dp。
dpi s 的前i个字符(1-indexed)有多少种拆法。考虑最后一个单词的长度,得到转移方程:

dpi=s[j+1,i]dpj

如果brute force的话,这个 dp O(nl2S) 的( l 为字典中单词的长度),当然你也可以用二分优化查询,不过也会TLE。
用Trie优化“查询s[j+1,i]是否在字典中”过程,复杂度降到 O(nl2) ,也就是 3e9 ,限时3s竟然过了。。。撒花~(≧▽≦)/~

UPD优化的解法

代码

#include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
const int maxn = 3e5+5;
const int maxn2 = 400000+8;
const int maxm = 26;
const int maxn3 = 105;
const int M = 20071027;
char s[maxn], s2[maxn3];
struct Trie
{
    int ch[maxn2][maxm];
    bitset<maxn2> val;
    int tot = 1;
    void init()
    {
        memset(ch[0], 0, sizeof ch[0]);
        val.reset();
        tot = 1;
    }
    void add(char *s)
    {
        int n = strlen(s);
        int cur = 0, id;
        for(int i = 0; i < n; ++i)
        {
            id = s[i] - 'a';
            if(!ch[cur][id])
            {
                memset(ch[tot], 0, sizeof ch[tot]);
                ch[cur][id] = tot++;
            }
            cur = ch[cur][id];
        }
        val[cur] = 1;
    }
    bool finda(char *s)
    {
        int n = strlen(s);
        int cur = 0, id;
        for(int i = 0; i < n; ++i)
        {
            id = s[i] - 'a';
            if(!ch[cur][id]) return false;
            cur = ch[cur][id];
        }
        return val.test(cur);
    }
}tr;
LL dp[maxn];
int main()
{
    int Case = 1;
    while(~scanf("%s", s+1))
    {
        int n;
        cin >> n;
        int len = strlen(s+1);
        tr.init();
        int mxLen = -1;
        for(int i = 0; i < n; ++i)
        {
            scanf("%s", s2);
            mxLen = max(mxLen, (int)strlen(s2));
            tr.add(s2);
        }
        memset(dp, 0, sizeof dp);
        dp[0] = 1;
        memset(s2, 0, sizeof s2);
        for(int i = 1; i <= len; ++i)
        {
            int bound = max(1, i - mxLen + 1);
            int cnt = 0;
            for(int j = i; j >= bound; --j)
            {
                for(int k = j; k <= i; ++k) s2[k - j] = s[k];
                s2[i - j + 1] = '\0';
                if(tr.finda(s2))
                    dp[i] = (dp[i] + dp[j-1]) % M;
            }
        }
        printf("Case %d: %d\n", Case++, dp[len]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值