LA 3942 - Remember the Word
来源:互联网 发布:preview.exe软件下载 编辑:程序博客网 时间:2024/05/22 07:47
Time limit: 3.000 seconds
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 , 1S4000 .
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
Trie树。
#include<cstdio>#include<cstring>using namespace std;const int MAXN=300111;char S[MAXN];int d[MAXN];int ch[400111][26];int value[400111];int sz;void word_insert(char *word){int i,u=1;int d=strlen(word);for(i=0;i<d;i++){if(ch[u][word[i]-'a']==0){ch[u][word[i]-'a']=++sz;memset(ch[sz],0,sizeof(ch[sz]));u=sz;value[u]=0;}else{u=ch[u][word[i]-'a'];}}value[u]=true;}int main(){int n,i,j,len,u,tmp;int t=1;char word[111];while(scanf("%s",S)==1){scanf("%d",&n);sz=1;value[1]=false;memset(ch[1],0,sizeof(ch[0]));for(i=1;i<=n;i++){scanf("%s",word);word_insert(word);}getchar();len=strlen(S);memset(d,0,sizeof(d));d[0]=1;for(i=0;i<len;i++){u=1;j=0;tmp=i;while(tmp<len && ch[u][S[tmp]-'a']!=0){j++;if(value[ch[u][S[tmp]-'a']])d[i+j]=(d[i+j]+d[i])%20071027;u=ch[u][S[tmp]-'a'];tmp++;}}printf("Case %d: %d\n",t++,d[len]);}return 0;}
- LA 3942 Remember the Word
- LA 3942 - Remember the Word
- LA 3942 - Remember the Word
- LA 3942 Remember the Word
- LA 3942 Remember the Word
- LA-3942 Remember the Word
- LA 3942 Remember the Word
- LA 3942 3942 - Remember the Word
- Remember the word, LA 3942, Trie Tree
- UVA 1401 Remember the Word & LA 3942
- LA 3942 Remember the Word(Trie+DP)
- LA 3942 Remember the Word (Trie)
- Trie字符串(Remember the Word, LA 3942)
- LA 3942 - Remember the Word 字典树 dp
- LA 3942 Remember the Word(前缀树&树上DP)
- LA 3942 - Remember the Word 字典树+DP
- LA 3942 Remember the Word / 前缀树 + DP
- LA ——3942 - Remember the Word(Trie 入门)
- 基于数据挖掘的商业银行客户关系管理系统应用研究
- MATLAB 线性回归
- 递归思想实现全排列
- 我为什么不考研(一)
- 数学专项number_theory:UVa 10539
- LA 3942 - Remember the Word
- 问题一百二十三:统计子串
- 关于派生类成员变量的内存问题
- 数学专项number_theory:UVa 11105
- hdu3336-Count the string
- n进制小数 将任意十进制正小数分别转换成2,3,4,5,6,7,8,9进制正小数,小数点后保留8位,并输出。
- HDU 3342
- ROS探索总结(四)——简单的机器人仿真
- KMP算法