【字典树】【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

扫描二维码关注公众号,回复: 2666803 查看本文章

题意:

我们要统计给出的字符串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;
}

猜你喜欢

转载自blog.csdn.net/Floraqiu/article/details/81544559