uvalive3942(字典树)
来源:互联网 发布:淘宝上组装电脑 编辑:程序博客网 时间:2024/06/07 13:19
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.
定义d[i]表示i到L(L是给出的s的长度)的分解个数,然后d[i]=sum{d[i+len(x)]},用x的时候在trie里面查找就好了
#include<set>#include<queue>#include<cmath>#include<cstdio>#include<vector>#include<string>#include<utility>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define Inf (1<<30)#define LL long long#define MOD 20071027#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;char s[300010];int dp[4000010][26],cnt;LL d[410000],val[4000010];void insert(char *str){ int L=strlen(str),u=0; for(int i=0;i<L;i++) { int in=str[i]-'a'; if(dp[u][in]==-1) { dp[u][in]=cnt; u=cnt++; } else u=dp[u][in]; if(i==L-1)val[u]++; }}LL query(char *str,int L,int x){ int u=0; LL ans=0; L=min(101,L); for(int i=0;i<L;i++) { int in=str[i]-'a'; if(dp[u][in]==-1)break; else if(val[dp[u][in]])ans=(ans+d[x+i+1])%MOD; u=dp[u][in]; } return ans;}int main(){ int T,o=1; int n,m; //freopen("D:\\oo.txt","r",stdin); while(~scanf("%s",s)) { cnt=1; scanf("%d",&n); memset(dp,-1,sizeof(dp)); memset(val,0,sizeof(val)); for(int i=0;i<n;i++) { char str[110]; scanf("%s",str); insert(str); } int L=strlen(s); d[L]=1; for(int i=L-1;i>=0;i--) d[i]=query(s+i,L-i,i); printf("Case %d: %lld\n",o++,(d[0]%MOD+MOD)%MOD); } return 0;}
- uvalive3942(字典树)
- 【Uvalive3942】Remember the Word——字典树+dp
- uvalive3942(前缀树)
- UVaLive3942
- uvalive3942 Remember the Word
- [UVALive3942] Remember the Word && 字符串
- Codevs 4189 字典(字典树Trie)
- 字典树(边改边抄)
- 字典树(Trie)
- 字典树(TrieTree)
- Trie(字典)树
- 字典树(非)
- hdu1251(字典树)
- POJ2503_Babelfish(字典树)
- hdu1671( 字典树)
- 字典树(Tries)
- 字典树(模板)
- HDU_2846_Repository(字典树)
- pl sql 学习笔记(六)再学动态sql
- js四舍五入(round()方法)并保留小数点后N位的函数
- 在servlet通过xml文件访问properties文件
- xxx
- 项目经验:clear清除浮动样式
- uvalive3942(字典树)
- 一个C源文件到可执行文件 [反汇编-函数栈帧 编译 链接]
- java中的引用类型
- 正则非贪婪匹配 及 容易出现的错误
- USB Class Codes
- 不要做一个浮躁的程序员
- Android ActionBar应用实战,高仿微信主界面的设计
- [转]推荐系统开源软件列表汇总和点评
- [iOS] 设置文件的ARC状态