[Trie树][dp] LA3942 Remember the Word 真·解法
来源:互联网 发布:开淘宝网店的流程 编辑:程序博客网 时间:2024/05/29 11:42
在上一篇的解法中,我们使用Trie树优化的dp,复杂度达到
Trie树是从根节点向下查找,如果我们计算的顺序和它一致,会大大降低复杂度。
重新定义
则,
这样对于
#include<bits/stdc++.h>typedef long long LL;typedef unsigned long long ull;using namespace std;const int maxn = 3e5+5;const int maxn2 = 400000+8;const int maxm = 26;const int maxn3 = 105;const int M = 20071027;char s[maxn], s2[maxn3];struct Trie{ int ch[maxn2][maxm]; bitset<maxn2> val; int tot = 1; void init() { memset(ch[0], 0, sizeof ch[0]); val.reset(); tot = 1; } void add(char *s) { int n = strlen(s); int cur = 0, id; for(int i = 0; i < n; ++i) { id = s[i] - 'a'; if(!ch[cur][id]) { memset(ch[tot], 0, sizeof ch[tot]); ch[cur][id] = tot++; } cur = ch[cur][id]; } val[cur] = 1; } bool finda(char *s) { int n = strlen(s); int cur = 0, id; for(int i = 0; i < n; ++i) { id = s[i] - 'a'; if(!ch[cur][id]) return false; cur = ch[cur][id]; } return val.test(cur); }}tr;LL dp[maxn];int main(){ int Case = 1; while(~scanf("%s", s+1)) { int n; cin >> n; int len = strlen(s+1); tr.init(); for(int i = 0; i < n; ++i) { scanf("%s", s2); tr.add(s2); } memset(dp, 0, sizeof dp); dp[len + 1] = 1; for(int i = len; i >= 1; --i) { int cur = 0, id; for(int j = i; j <= len; ++j) { id = s[j] - 'a'; if(!tr.ch[cur][id]) break; cur = tr.ch[cur][id]; if(tr.val[cur]) dp[i] = (dp[i] + dp[j+1])%M; } } printf("Case %d: ", Case++); cout << dp[1] << endl; } return 0;}
阅读全文
0 0
- [Trie树][dp] LA3942 Remember the Word 真·解法
- [Trie树][dp] LA3942 Remember the Word
- LA3942 remember the word trie+dp
- LA3942 Remember the Word(Trie+DP)
- 【LA3942】Remember the Word【Trie】【计数DP】
- LA3942 Remember the Word(Trie+DP)
- Remember the Word,LA3942(Trie树+DP)
- 【Trie的模板】例题11 LA3942 Remember the Word(字典树+dp)
- LA3942 Remember the Word(字典树+DP)
- Remember the Word LA3942
- Uva1401/LA3942 Remember the Word(trie模板)
- UVA 1401 Remember the Word(DP+字典树Trie)
- Remember the Word - UVaLive 3942 Trie树+dp
- uva 1401Remember the Word(trie树+dp)
- (LA 3942)Remember the Word --DP+Trie树
- UVA 1401 Remember the Word(Trie树 套 DP)
- LA 3942 Remember the Word Trie树 DP
- UVA 1401 Remember the Word(DP+字典树Trie)
- win10环境下实现python3.5与python2.7共存
- Java面试题集
- 标准流
- 二维数组
- L1-020. 帅到没朋友
- [Trie树][dp] LA3942 Remember the Word 真·解法
- C Primer Plus(第6版)第九章答案
- 前端学习笔记--AJAX的应用(三)form表单改为AJAX提交
- 怎么查找bug
- css_day04_margin垂直方向的合并现象
- 【分布式存储项目】(8)redis简介及配置
- USACO-Section2.2 Runaround Numbers【暴力枚举】
- 文章标题
- 友善之臂视频监控方案源码学习(1)