题目传送门

题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个

分析:书上的做法。递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+len(s)])。用字典树记录并查询短单词的前缀的长度。

#include <bits/stdc++.h>
using namespace std; const int L = 3e5 + 5;
const int N = 4e3 + 5;
const int M = 1e2 + 5;
const int MOD = 20071027;
char text[L], word[M];
struct Trie {
int ch[N*M][26], val[N*M], sz;
int idx(char c) {
return c - 'a';
}
void clear(void) {
sz = 1; memset (ch[0], 0, sizeof (ch[0]));
}
void insert(char *str, int v) {
int u = 0, len = strlen (str);
for (int c, i=0; i<len; ++i) {
c = idx (str[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] = v;
}
void query(char *str, int len, vector<int> &vec) {
int u = 0;
for (int c, i=0; i<len; ++i) {
c = idx (str[i]);
if (!ch[u][c]) return ;
u = ch[u][c];
if (val[u]) vec.push_back (val[u]);
}
}
}trie;
int dp[L], l[N]; int main(void) {
int cas = 0, n;
while (scanf ("%s", &text) == 1) {
scanf ("%d", &n);
trie.clear ();
for (int i=1; i<=n; ++i) {
scanf ("%s", &word);
l[i] = strlen (word);
trie.insert (word, i);
}
memset (dp, 0, sizeof (dp));
int len = strlen (text); dp[len] = 1;
for (int i=len-1; i>=0; --i) {
vector<int> vec;
trie.query (text+i, len-i, vec);
for (int j=0; j<vec.size (); ++j) {
dp[i] = (dp[i] + dp[i+l[vec[j]]]) % MOD;
}
}
printf ("Case %d: %d\n", ++cas, dp[0]);
} return 0;
}

  

Trie + DP LA 3942 Remember the Word的更多相关文章

  1. LA 3942 Remember the Word(前缀树&树上DP)

    3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...

  2. [LA 3942] Remember the Word

    Link: LA 3942 传送门 Solution: 感觉自己字符串不太行啊,要加练一些蓝书上的水题了…… $Trie$+$dp$ 转移方程:$dp[i]=sum\{ dp[i+len(x)+1]\ ...

  3. UVA - 1401 | LA 3942 - Remember the Word(dp+trie)

    https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...

  4. LA 3942 - Remember the Word 字典树+DP

    看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  5. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  7. UVALive - 3942 Remember the Word (Trie + DP)

    题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...

  8. UVA 3942 Remember the Word (Trie+DP)题解

    思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...

  9. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

随机推荐

  1. C++静态代码分析工具对比cppCheck与PreFast

    具体内容参看文件<CppCheck和PreFast对Cplusplus代码静态分析测试.zip> C++测试源代码main.cpp #define NULL 0 #include < ...

  2. 奶牛健美操(codevs 3279)

    题目描述 Description Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点 ...

  3. iOS 设备的屏幕尺寸一览

    最近在做iPad开发项目 iPhone 设备: iPhone 1G --- 320x480iPhone 3G --- 320x480iPhone 3GS --- 320x480iPhone 4 --- ...

  4. Android手绘效果实现

    效果图 原理 大概介绍一下实现原理.首先你得有一张图(废话~),接下来就是把这张图的轮廓提取出来,轮廓提取算法有很多,本人不是搞图像处理的,对图像处理感兴趣的童鞋可以查看相关资料.如果你有好的轮廓提取 ...

  5. php 用户登录验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Ubuntu16.04 + Win 10 双系统 时间同步,启动项顺序,NumLock指示灯常亮

    1. Ubuntu & win10 双系统时间同步: 先在ubuntu下更新一下时间,确保时间无误: sudo apt-get install ntpdate sudo ntpdate tim ...

  7. 【mysql创建用户|删除用户|修改用户权限|常用命令】

    原文链接:http://blog.csdn.net/leili0806/article/details/8573636 1.       CREATE USER 语法: CREATE USER 'us ...

  8. 解决css样式被内置样式覆盖的问题

    .preImg { height:400px !important } <img id="preImg" class="preImg" style=&qu ...

  9. LSM-Tree (BigTable 的理论模型)(转)

    Google的BigTable架构在分布式结构化存储方面大名鼎鼎,其中的MergeDump模型在读写之间找到了一个较好的平衡点,很好的解决了web scale数据的读写问题. MergeDump的理论 ...

  10. MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏

    我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...