trie+dp

本文介绍了一种解决特定组合问题的算法实现,该问题要求将一个字符串分解为给定字典中单词的所有可能组合,并计算出所有有效组合的数量。通过使用前缀树(Trie)数据结构来存储字典单词,结合动态规划方法来高效地解决问题。

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

Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowingthat 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’sonly 20071027 sticks, he can only record the remainders of the numbers divided by total amount ofsticks.The problem is as follows: a word needs to be divided into small pieces in such a way that eachpiece is from some given set of words. Given a word and the set of words, Jiejie should calculate thenumber 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 wordwhose 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 100characters 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


#define  MAX 26

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
using namespace std;
 struct Trie
{
bool v; 
Trie *next[26];
void clear()
{
memset(next,0,sizeof next);
v=false;
}
}trie[400004];


char str[400004],tmp[104];
int n,tot,dp[400004];


void createTrie()
{
// int length = strlen(str);
Trie *p = &trie[0];
for(int i = 0;tmp[i];i++)
{
int id = tmp[i]-'a';
if(p->next[id] == NULL)
{
trie[tot].clear();
p->next[id] = &trie[tot++];
}

p = p->next[id];
}
p->v=true;
}
int l;
void  FindTrie(int add,int jmp)
{


Trie *p = &trie[0];
for(int i = jmp ;i<=l;i++)
{
if(p->next[str[i]-'a'])
{
p = p->next[str[i] - 'a'];
if(p->v)
dp[i]=(dp[i]+add)%20071027;
}
else
break;
}

}
int main()
{
int cas=1;
while(~scanf("%s",&str[1]))
{

scanf("%d",&n);
trie[0].clear();
tot=1;
for(int i=0;i<n;i++)
{
scanf("%s",tmp);
createTrie();
}
memset(dp,0,sizeof dp);
dp[0]=1;
l=strlen(&str[1]);
for(int i=1;i<=l;i++)
if(dp[i-1])
FindTrie(dp[i-1],i);

printf("Case %d: %d\n",cas++,dp[l]);
}
return 0;
}
# P4052 [JSOI2007] 文本生成器 ## 题目描述 JSOI 交给队员 ZYX 一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是 GW 文本生成器 v6 版。 该软件可以随机生成一些文章——总是生成一篇长度固定且完全随机的文章。 也就是说,生成的文章中每个字符都是完全随机的。如果一篇文章中至少包含使用者们了解的一个单词,那么我们说这篇文章是可读的(我们称文章 $s$ 包含单词 $t$,当且仅当单词 $t$ 是文章 $s$ 的子串)。但是,即使按照这样的标准,使用者现在使用的 GW 文本生成器 v6 版所生成的文章也是几乎完全不可读的。ZYX 需要指出 GW 文本生成器 v6 生成的所有文本中,可读文本的数量,以便能够成功获得 v7 更新版。你能帮助他吗? 答案对 $10^4 + 7$ 取模。 ## 输入格式 第一行有两个整数,分别表示使用者了解的单词总数 $n$ 和生成的文章长度 $m$。 接下来 $n$ 行,每行一个字符串 $s_i$,表示一个使用者了解的单词。 ## 输出格式 输出一行一个整数表示答案对 $10^4 + 7$ 取模的结果。 ## 输入输出样例 #1 ### 输入 #1 ``` 2 2 A B ``` ### 输出 #1 ``` 100 ``` ## 说明/提示 #### 数据规模与约定 对于全部的测试点,保证: - $1 \leq n \leq 60$,$1 \leq m \leq 100$。 - $1 \leq |s_i| \leq 100$,其中 $|s_i|$ 表示字符串 $s_i$ 的长度。 - $s_i$ 中只含大写英文字母。 为什么WA了(过了样例): ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e4 + 7; const int MAX_N = 100; const int LENGTH = 10050; int n, m, trie[LENGTH][26], tot; int fail[LENGTH], dp[MAX_N][LENGTH]; bool vis[LENGTH]; void insert(string s) { int cur = 0; for (char ch : s) { int i = ch - 'A'; if (!trie[cur][i]) trie[cur][i] = ++tot; cur = trie[cur][i]; } vis[cur] = true; } void build_fail() { queue<int> q; for (int i = 0; i < 26; i++) if (trie[0][i]) q.push(trie[0][i]); while (!q.empty()) { int cur = q.front(); q.pop(); vis[cur] |= vis[fail[cur]]; for (int i = 0; i < 26; i++) if (trie[cur][i]) { fail[trie[cur][i]] = trie[fail[cur]][i]; q.push(trie[cur][i]); } else trie[cur][i] = trie[fail[cur]][i]; } } int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { string s; cin >> s; insert(s); } build_fail(); dp[0][0] = 1; for (int i = 0; i < n; i++) for (int j = 0; j <= tot; j++) for (int k = 0; k < 26; k++) if (!vis[trie[j][k]]) dp[i + 1][trie[j][k]] = (dp[i + 1][trie[j][k]] + dp[i][j]) % MOD; int ans = 1; for (int i = 1; i <= n; i++) ans = ans * 26 % MOD; for (int i = 0; i <= tot; i++) ans = (ans - dp[n][i]) % MOD; cout << (ans + MOD) % MOD << '\n'; return 0; } ```
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值