UVA 1401 Remember the Word (trie + dp)
来源:互联网 发布:linux启动停在进度条 编辑:程序博客网 时间:2024/05/07 07:20
第一次写trie, 看别人的代码看懂的,顺便学了一下dp。
#include <iostream>#include <cstring>#include <cstdio>#include <vector>#define mod 20071027;using namespace std;char str[300010], s[110];int dp[300010];struct Trie{ int ch[400010][26]; int val[400010]; int sz; void clearr() { sz = 1; memset(ch, 0, sizeof(ch)); } int idc(char c) { return c - 'a'; } void insertt(char *s, int lenn) { int u = 0; for(int i = 0; i < lenn; i++) { int c = idc(s[i]); if(!ch[u][c]) { val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } val[u] = lenn; } void find_prefix(char *s, int len, vector<int>& ans) { int u = 0; for(int i = 0; i < len; i++) { int c = idc(s[i]); if(ch[u][c]) { u = ch[u][c]; if(val[u]) ans.push_back(val[u]); } else break; } }}test;int main(){ int kase = 0; while(~scanf("%s", str)) { memset(dp, 0, sizeof(dp)); int len = strlen(str); test.clearr(); int S; scanf("%d", &S); for(int i = 1; i <= S; i++) { scanf("%s", s); int lenn = strlen(s); test.insertt(s, lenn); } dp[len] = 1; for(int i = len - 1; i >= 0; i--) { vector<int> ans; test.find_prefix(str + i, len - i, ans); for(int j = 0; j < ans.size(); j++) { dp[i] = (dp[i] + dp[i + ans[j]]) % mod; } } printf("Case %d: %d\n", ++kase, dp[0]); }}
0 0
- UVA 1401 - Remember the Word(Trie+DP)
- uva 1401 Remember the Word ( Trie + DP )
- UVA 1401 Remember the Word (trie + dp)
- UVa 1401 Remember the Word(Trie+DP)
- UVA 1401 Remember the Word(DP+字典树Trie)
- uva 1401 Remember the Word(Trie dp)
- UVa 1401 - Remember the Word(Trie + DP)
- uva 1401Remember the Word(trie树+dp)
- UVA 1401 Remember the Word(Trie树 套 DP)
- UVA 1401 Remember the Word(DP+字典树Trie)
- Uva 1401 - Remember the Word//TRie
- UVA-1401-Remember the Word(Trie)
- UVA-1401 - Remember the Word -----Trie前缀树
- UVa 1401 Remember the Word (Trie树模板题)
- UVA 3942 - Remember the Word (Trie)
- UVA 3942 Remember the Word(Trie树)
- LA3942 remember the word trie+dp
- LA 3942 Remember the Word(Trie+DP)
- xmind思维导图集合
- cocos2d-x 进步之路 资源释放的原理和最好时机
- UITextField
- eclipse开发代码集合
- 工作学习中参考过的一些网站(多数为外文)
- UVA 1401 Remember the Word (trie + dp)
- 01 反射参数问题
- 浏览器打开HTML页面(UTF-8编码)是总是乱码
- eclipse android 项目中raw中的文件拿不到
- github集合
- centos简介与VMware安装
- Webstorm 10 for mac osx 注册机,序列号,kegen
- 接口测试集合
- 三色球