LA 3942 - Remember the Word 字典树 dp

本文讨论了如何利用字典树解决组合问题,特别是关于如何将一个长字符串分解成给定字典中单词的链接,计算可能的分解方式数量,并通过实例展示了算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Sample Input 

abcd 
4 
a 
b 
cd 
ab

Sample Output 

Case 1: 2

------------------------------------------------------------
给出一个由S个不同单词组成的字典和一个长字符串,把这个字符串分解成若干个单词的链接,单词可以重复使用,有多少种方法?
f[i]表示以字符i为结尾的单词分解方法,若一个长度为x的单词与以i+1为开头的部分相匹配,则f[i+x]=f[i+x]+f[i];
将单词建立一个字典树,用字典树快速求出匹配的单词。


------------------------------------------------------------
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>

using namespace std;

char str[333333];
char words[4444][111];
int f[333333];

//用类,或者结构体定义都行
struct trie
{
    trie* next[26];
    int num;
    int value;
    trie()
    {
       for(int i=0;i<26;i++) next[i]=NULL;
       value=0;//记录是不是一个单词
       num=0;//记录单词出现的次数
    }
    void clear()
    {
       for(int i=0;i<26;i++) next[i]=NULL;
       value=0;//记录是不是一个单词
       num=0;//记录单词出现的次数
    }
}root;

//插入:
void insert(char* s)
{
    trie* p=&root;
    int k=0;
    while(s[k]!='\0')
    {
        if(!p->next[s[k]-'a']) p->next[s[k]-'a']=new trie;
        p=p->next[s[k]-'a'];
        p->num++;
        k++;
    }
    p->value=1;
}

//查找
void find(char* s,int pos)
{
    trie* p=&root;
    int k=0;
    while(s[k]!='\0'&&p->next[s[k]-'a'])
    {
        p=p->next[s[k]-'a'];
        if (p->value==1)
        {
            f[pos+k+1]=(f[pos+k+1]+f[pos])%20071027;
        }
        k++;
    }
}

int main()
{
    int l,s;
    int cnt=0;
    while (~scanf("%s",str+1))
    {
        cnt++;
        memset(f,0,sizeof(f));
        root.clear();
        scanf("%d",&s);
        l=strlen(str+1);
        for (int i=0;i<s;i++)
        {
            scanf("%s",words[i]);
            insert(words[i]);
        }
        f[0]=1;
        for (int i=1;i<=l;i++)
        {
            if (f[i-1])
            {
                find(str+i,i-1);
            }
        }
        printf("Case %d: %d\n",cnt,f[l]);
    }
    return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值