【字符串·Trie】uva3942 Remember the words
来源:互联网 发布:淘宝联盟网页版怎么用 编辑:程序博客网 时间:2024/05/19 20:43
遇到多校第五场,1002,于是开始学trie树和AC自动机;
模板用的挑程,Trie树用一个二维数组ch[节点数目][字符种类数]实现; ch[I][j]表示第i个节点伸向j字符,如果存在字符j接在节点i之后,则ch[I][j]>0;
首先一道模板题uva3942
用d[I]表示 后缀I-L的分解方案数
d[I] = sum{d[I+len(x)]} 其中len(x) 是合法单词且是I-L的前缀;
以上抄的刘汝佳orz
我的理解是,从某个点i开始(包括i)某个前缀刚好是单词,就可以把单词结尾处的d[结尾点]加到d[I]上;
#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;typedef long long ll;const int maxn = 300010;const int mod = 20071027;//建树int ch[maxn][26];int val[maxn];int sz;void init(){ sz = 1; memset(ch[0], 0, sizeof(ch[0])); }int idx(char c){ return c - 'a'; }int d[maxn];char t[maxn];void insert(char *s){ int u = 0, n = strlen(s); for (int i = 0; i < n; i++){ int c = idx(s[i]); if (!ch[u][c]){ memset(ch[sz], 0, sizeof(ch[sz])); val[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } val[u] = 1;}void query(int id,char *s){ d[id] = 0; int u = 0; for (int i = id; s[i]; i++){ int c = idx(s[i]); if (!ch[u][c]) return; u = ch[u][c]; //如果是单词节点 if (val[u]) d[id] = (d[id] + d[i + 1]) % mod; }}int n;void solve(char *s){ int len = strlen(s); d[len] = 1; //从后向前计算d[I]; for (int i = len - 1; i >= 0; i--){ query(i, s); }}char s[110];int main(){ int kase = 0; while (~scanf("%s", t)){ init(); scanf("%d", &n); for (int i = 1;i<=n;i++){ scanf("%s", s); insert(s); } solve(t); printf("Case %d: %d\n", ++kase,d[0]); }}
阅读全文
0 0
- 【字符串·Trie】uva3942 Remember the words
- Trie字符串(Remember the Word, LA 3942)
- Remember the Word (trie)
- Remember The Word-Trie
- [Trie树][dp] LA3942 Remember the Word 真·解法
- Remember the word, LA 3942, Trie Tree
- Uva 1401 - Remember the Word//TRie
- LA3942 remember the word trie+dp
- LA 3942 Remember the Word(Trie+DP)
- UVALive - 3942 Remember the Word (Trie)
- UVA-1401-Remember the Word(Trie)
- UVA 1401 - Remember the Word(Trie+DP)
- uvalive 3942 - Remember the Word(Trie)
- LA 3942 Remember the Word (Trie)
- LA3942 Remember the Word(Trie+DP)
- uva 1401 Remember the Word ( Trie + DP )
- UVALive - 3942 Remember the Word Trie
- UVA 3942 - Remember the Word (Trie)
- Redis命令
- 基于人机交互设备测量用户情绪。
- UVA10820SendATable
- 【我很简单,请不要欺负我】模拟环境渗透详细记录
- 搭建Hadoop开发环境
- 【字符串·Trie】uva3942 Remember the words
- STL——set
- connect()函数阻塞和非阻塞问题解决
- C语言小项目-万年历
- Scala学习之三Scala_ 下划线占位符语法
- MBR 和 GPT 分区差别
- linux-关机重启命令
- 我理解的设计模式之适配器模式
- 小明左手拿着纸牌黑桃10,右手拿着纸牌红桃8, 现在交换手中的牌, 用程序模拟实现的过程, 并输出交换前后手中的纸牌的结果