UVALive 3942 (LA 3492) Remember the Word Trie树 + 记忆化搜索
来源:互联网 发布:网络信息安全法 编辑:程序博客网 时间:2024/05/17 04:52
题目大意:
白书例题
给出由S个不同单词组成的字典和一个长字符串. 把这个字符串分解成若干个单词的连接, 单词可以重复使用, 问有多少种分解方法
单词个数1 <= S <=4000, 每个单词长度不超过100, 给出的长字符串长度不超过300000
大致思路:
首先将S个单词插入Trie树, 然后利用递推的思想记忆化搜索即可
代码如下:
Result : Accepted Memory : ? KB Time : 79 ms
/* * Author: Gatevin * Created Time: 2015/2/11 13:44:36 * File Name: Mononobe_Mitsuki.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;/* * 白书例题, 先建立S个单词的Trie树然后在Trie树上递推记忆化搜索 */const int mod = 20071027;int dp[300010];struct Trie{ int next[400010][26]; bool end[400010]; int L, root; int newnode() { for(int i = 0; i < 26; i++) next[L][i] = -1; end[L++] = 0; return L - 1; } void init() { L = 0; root = newnode(); return; } void insert(char *s) { int now = root; for(; *s; s++) { if(next[now][*s - 'a'] == -1) next[now][*s - 'a'] = newnode(); now = next[now][*s - 'a']; } end[now] = 1; return; } int find(char *s, int num)//搜索后缀s位置开始的组成方法数 { if(dp[num] != -1) return dp[num];//记忆化搜索 int tnum = num; int ret = 0; int now = root; for(; *s; s++, num++) { if(next[now][*s - 'a'] == -1) break; now = next[now][*s - 'a']; if(end[now]) { if(*(s + 1) == '\0') ret = (ret + 1) % mod; else ret = (ret + find(s + 1, num + 1)) % mod; } } dp[tnum] = ret; return ret; }};Trie trie;char S[300010];char word[110];int main(){ int n; int cas = 1; while(scanf("%s", S) != EOF) { trie.init(); scanf("%d", &n); while(n--) { scanf("%s", word); trie.insert(word);//建立Trie树 } int N = strlen(S); for(int i = 0; i < N; i++) dp[i] = -1; int ans = trie.find(S, 0); printf("Case %d: %d\n", cas++, ans); } return 0;}
0 0
- UVALive 3942 (LA 3492) Remember the Word Trie树 + 记忆化搜索
- Remember the word, LA 3942, Trie Tree
- LA 3942 Remember the Word(Trie+DP)
- LA 3942 Remember the Word (Trie)
- Trie字符串(Remember the Word, LA 3942)
- UVALive - 3942 Remember the Word (Trie)
- uvalive 3942 - Remember the Word(Trie)
- UVALive - 3942 Remember the Word Trie
- UVALive - 3942 Remember the Word(trie + dp)
- (LA 3942)Remember the Word --DP+Trie树
- LA 3942 Remember the Word Trie树 DP
- Remember the Word - UVaLive 3942 Trie树+dp
- UVALive - 3942 - Remember the Word (Trie树)
- UVALive 3942 Remember the Word 前缀树Trie
- LA 3942(Remember the Word-Trie)[Template:Trie]
- LA ——3942 - Remember the Word(Trie 入门)
- LA3942 Remember the Word(字典树+记忆化搜索)
- UVALive 3942 Remember the Word(trie + dp)
- ViewPager+Fragment实现TabHost(可复用的类)
- zoj 1383 Binary Numbers
- c#网络通信框架networkcomms内核解析之二 消息处理流程
- java Junit的各种断言
- 手把手教你使用start uml画用例图
- UVALive 3942 (LA 3492) Remember the Word Trie树 + 记忆化搜索
- poj 2299 又是逆序数。。。
- Android微信式底部标签栏和fragment切换实现
- FCKeditor html 文本编辑器的配置应用
- 即时通讯 音视频 开发技术
- 好记性不如烂笔头50-javaWeb框架struts2中的方法拦截器(5)
- 个人总结之类 Arrays类的常用方法
- hdu 5024 枚举
- 灰度图像--图像分割 Scharr算子