UVALive - 3942 Remember the Word(字典树+dp)
2015-02-15 18:09
316 查看
Remember the Word
Submit Status
Description
![](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
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
![](https://icpcarchive.ecs.baylor.edu/external/39/3942img1.png)
S
![](https://icpcarchive.ecs.baylor.edu/external/39/3942img1.png)
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
Sample Output
Source
Regionals 2007 >> Asia
- Nanjing
分析:
大白书题目。字典树+dp,初学是很难的题目。好好理解!
ac代码:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 400000
const int mod=20071027;
struct node
{
int ch[27];
int v; // 0表示不是单词,其余数字表示为一个单词。
node()
{
memset(ch,0,sizeof(ch));
v=0;
}
}tree[MAXN];
int tot;
char op[300010];
char word[100];
int d[300010];
void insert(int v)
{
int len=strlen(word);
int root=0;
for(int i=0;i<len;i++)
{
if(tree[root].ch[word[i]-'a']==0) tree[root].ch[word[i]-'a']=tot++;
root=tree[root].ch[word[i]-'a'] ;
}
tree[root].v=v;
}
void cheak(int i,int len)
{
int ans=0;
int root=0;
for(int j=i;j<=len;j++)
{
if(tree[root].ch[op[j]-'a']==0) break;
root=tree[root].ch[op[j]-'a'];
if(tree[root].v) ans+=d[j+1]%mod;//dp思想
ans%=mod;
}
d[i]=ans;
}
int main()
{
int kase=1;
while(~scanf("%s",op))
{
memset(tree,0,sizeof(tree));
int len=strlen(op);
tot=1;
int s;
scanf("%d",&s);
for(int i=1;i<=s;i++)
{
scanf("%s",word);
insert(i);
}
memset(d,0,sizeof(d));
d[len]=1; // 初始化边界
for(int i=len-1;i>=0;i--)
{
cheak(i,len-1);
}
printf("Case %d: %d\n",kase++,d[0]%mod);
}
}
/**
abcd
4
a
b
cd
ab
abcd
0
**/
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
![](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
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
![](https://icpcarchive.ecs.baylor.edu/external/39/3942img1.png)
S
![](https://icpcarchive.ecs.baylor.edu/external/39/3942img1.png)
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
Source
Regionals 2007 >> Asia
- Nanjing
分析:
大白书题目。字典树+dp,初学是很难的题目。好好理解!
ac代码:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 400000
const int mod=20071027;
struct node
{
int ch[27];
int v; // 0表示不是单词,其余数字表示为一个单词。
node()
{
memset(ch,0,sizeof(ch));
v=0;
}
}tree[MAXN];
int tot;
char op[300010];
char word[100];
int d[300010];
void insert(int v)
{
int len=strlen(word);
int root=0;
for(int i=0;i<len;i++)
{
if(tree[root].ch[word[i]-'a']==0) tree[root].ch[word[i]-'a']=tot++;
root=tree[root].ch[word[i]-'a'] ;
}
tree[root].v=v;
}
void cheak(int i,int len)
{
int ans=0;
int root=0;
for(int j=i;j<=len;j++)
{
if(tree[root].ch[op[j]-'a']==0) break;
root=tree[root].ch[op[j]-'a'];
if(tree[root].v) ans+=d[j+1]%mod;//dp思想
ans%=mod;
}
d[i]=ans;
}
int main()
{
int kase=1;
while(~scanf("%s",op))
{
memset(tree,0,sizeof(tree));
int len=strlen(op);
tot=1;
int s;
scanf("%d",&s);
for(int i=1;i<=s;i++)
{
scanf("%s",word);
insert(i);
}
memset(d,0,sizeof(d));
d[len]=1; // 初始化边界
for(int i=len-1;i>=0;i--)
{
cheak(i,len-1);
}
printf("Case %d: %d\n",kase++,d[0]%mod);
}
}
/**
abcd
4
a
b
cd
ab
abcd
0
**/
相关文章推荐
- UVALive 3942 Remember the Word(字典树 + 简单dp)
- UVALive 3942 - Remember the Word(DP,字典树)
- UVALive 3942 Remember the Word(字典树+dp)
- uvalive 3942 Remember the Word (字典树+DP)
- UVALive - 5097(斜率DP)
- uvalive 3942(dp + trie)
- UVALive 4987---Evacuation Plan(区间DP)
- UVALive 7043 International Collegiate Routing Contest 字典树,递归
- 【区间dp】【记忆化搜索】UVALive - 3516 - Exploring Pyramids
- [UVALive4864] Bit Counting && 数位DP
- UVALive 5790 Ball Stacking DP
- UVALive 6912 Prime Switch 状压DP
- UVAlive11324 The Largest Clique(scc+dp)
- UVALive 3907 (LA 3907) Puzzle AC自动机 + 搜索DP 记录路径
- [UVALive 6661 Equal Sum Sets] (dfs 或 dp)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- UVALive 3782 Bigger is Better(数位DP + 大数)
- UVALive 5002/ lightoj 1382 The Queue(树形DP)
- UVALive - 4794 Sharing Chocolate DP
- UVAlive 2031 - Dance Dance Revolution(dp,黑书dp例四)