【字典树】【DP】UVALive - 3942 Remember the Word 【给长字符串str和n个单词,把该字符串分解成若干个单词的方法有几种】

【字典树】【DP】UVALive - 3942 Remember the Word 【给长字符串str和n个单词,把该字符串分解成若干个单词的方法有几种】

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 ≤ S ≤ 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

题意:

我们要统计给出的字符串s和若干个单词,有多少种方法把字符串拆成这些单词。字符串长度3*1e5,单词数4000,单词长度100。

思路:

  • 设字符串s的长度为len,末位元素为s[len],令dp[i]表示从s的第i位开始的字符串(s[i…len])的分解方案数
  • 初始化dp[len+1]=1,用于末位dp[len]的状态转移
  • 我们倒着枚举起始位置i(i: len -> 1),从字符串的第i位出发,用字符串s[i … len] 一直往后跑字典树。
  • 如果跑到j位时若找到了一个单词,长度为m,则说明从i到j这里可以划分成一个单词,加上后缀j+1的方案数:dp[i]+=dp[j+1](其中j = i + m - 1),也就是dp[i] += dp[i + m]
  • 如果跑到某个位置,发现s字符跑不下去了,说明这种分割方法不行,返回即可,因为返回就没有增加dp[i]。
  • 由于单词长度<=100,所以第i位最多往后跑100次,故复杂度为3*1e5*100,是可以接受的

AC代码:

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <set>
#include <map>
#include <bitset>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

const int MOD = 20071027;
const int maxn = 300005;
int dp[maxn];
char a[maxn], b[maxn];

struct trie
{
    int val;
    bool exist;
    trie * next[30];
    trie()
    {
        val = 0;
        exist = false;
        memset(next, 0, sizeof(next));
    }
};

void InsertTrie(trie * root, char *word)
{
    trie * node = root;
    int id;
    char *p = word;
    while(*p)
    {
        id = *p - 'a';
        if(!node->next[id])
        {
            node->next[id] = new trie;
        }
        node = node->next[id];
        p++;
    }
    node->val = strlen(word);
    node->exist = true;
}

void query(trie * root, char *str, int j)
{
    trie * node = root;
    int id;
    char * p = str;
    while(*p)
    {
        id = *p - 'a';
        if(node->next[id])
        {
            node = node->next[id];
            if(node->exist)
            {
                dp[j] = (dp[j] +  dp[j + node->val]) % MOD;
//                printf("# %d %d\n", j, dp[j]);
            }
        }
        else
            return ;
        p++;
    }
    return ;
}

void DeleteTrie(trie * node)
{
    for(int i = 0; i < 30; i++)
    {
        if(node->next[i])
            DeleteTrie(node->next[i]);
    }
    delete node;
}

int main()
{
    int ks = 1;
    while(~scanf("%s", a))
    {
        int n;
        scanf("%d", &n);
        trie * root = new trie;
        for(int i = 1; i <= n; i++)
        {
            scanf("%s", b);
            InsertTrie(root, b);
        }
        int len = strlen(a);
        memset(dp, 0, sizeof(dp));
        dp[len] = 1;
        for(int i = len - 1; i >= 0; i--)
        {
            query(root, a + i, i);
        }
        printf("Case %d: %d\n", ks++, dp[0]);
        DeleteTrie(root);
    }
    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、资源1项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值