LA3942 Remember the world
来源:互联网 发布:c4d注册机mac版 编辑:程序博客网 时间:2024/05/16 23:40
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
转载请注明出处:http://blog.csdn.net/scut_pein/article/details/19681339
题意:给出一个由S个不同的单词组成的字典和一个长字符串。把这个字符串分解成若干个单词的连接,有多少种方法?比如,有4个单词a,b,cd,ab,则abcd有两种分解方法:a+b+cd 和 ab+cd。
思路:最先想到的是记忆化搜索,dp[i]记录从i开始的分解种数,首先将单词插入trie,然后将母本在trie上跑,跑到标记为尾结点处则ans += dp[i+1]。(如果dp[i+1]没记录得先调用函数)。显然华丽丽地超时了、、、
然后想到从右往左递推,枚举长度1到100.然后在trie树上跑,如果能够跑到有标记的地方,则dp[i] += dp[i+j](j是枚举的长度)
注意初始化dp[len] = 1;
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 400080char T[maxn],str[4080];int dp[maxn];#define MOD 20071027struct Trie{int ch[maxn][26];bool vis[maxn];int cnt;void init(){memset(ch[0],0,sizeof(ch[0]));memset(vis,0,sizeof(vis));cnt = 1;}int idx(char c){return c - 'a';}void insert(char * s){int len = strlen(s);int u = 0;for(int i = 0;i < len;i++){int c = idx(s[i]);if(!ch[u][c]){memset(ch[cnt],0,sizeof(ch[cnt]));ch[u][c] = cnt++;}u = ch[u][c];}vis[u] = 1;}bool find(int pos,int len){int u = 0;for(int i = pos;i < pos+len;i++){int c = idx(T[i]);if(!ch[u][c])return 0;u = ch[u][c];}if(vis[u])return 1;return 0;}}trie;int main(){//freopen("in.txt","r",stdin);int cas = 0;while(scanf("%s",T)!=EOF){cas++;int n;scanf("%d",&n);trie.init();while(n--){scanf("%s",&str);trie.insert(str);}memset(dp,0,sizeof(dp));int len = strlen(T);dp[len] = 1;for(int i = len-1;i >= 0;i--){//枚举1到100看能否在树上找到有末尾标记的结点for(int j = 1;j <= 100;j++){if(trie.find(i,j)){dp[i] += dp[i+j];if(dp[i] >= MOD)dp[i] %= MOD;}}}printf("Case %d: %d\n",cas,dp[0]);}return 0;}
- LA3942 Remember the world
- Remember the Word LA3942
- LA3942 remember the word trie+dp
- LA3942 Remember the Word(Trie+DP)
- 【LA3942】Remember the Word【Trie】【计数DP】
- LA3942 Remember the Word(Trie+DP)
- [Trie树][dp] LA3942 Remember the Word
- Remember the Word,LA3942(Trie树+DP)
- LA3942 Remember the Word(字典树+记忆化搜索)
- LA3942 Remember the Word(字典树+DP)
- Uva1401/LA3942 Remember the Word(trie模板)
- [Trie树][dp] LA3942 Remember the Word 真·解法
- 【Trie的模板】例题11 LA3942 Remember the Word(字典树+dp)
- LA 3492 Remember the World(Trie + dp)
- LA3942
- remember the milk 快捷键
- Remember the Word (trie)
- 1401 - Remember the Word
- 心情真好
- 另类电商年入上亿美金,Amazon评论中掘金
- 页面iframe内查找关键字,从数据库读取相关文章
- 你是把我这里当跳板呢
- Tomcat 部署java项目三种方式
- LA3942 Remember the world
- 梦痕已逝
- 一个简单的100以内的猜字游戏
- 越来越慢的微信,请温习一下谷歌的极简追求
- 2014年全国计算机等级考试报考指南汇总
- 谷歌服务框架_谷歌服务框架下载_谷歌服务框架全版本整理
- jsp实现自动登录
- HTML5 canvas绘制arcTo、translate和rotate的画法探索
- ios中使用百度地图简单步骤