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

被折叠的 条评论
为什么被折叠?



