题目传送门

题意:(训练指南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. MFC CheckBox

    if ( BST_CHECKED == IsDlgButtonChecked( IDC_CHECK1 ) ){// 勾选}else{}

  2. 异常处理__try{}__except(EXCEPTION_EXECUTE_HANDLER){}

    在一个函数中不能混合使用 try{}catch(CException *e){} 与 __try{}__except(EXCEPTION_EXECUTE_HANDLER){} 编译时报错 error ...

  3. jquery中dom元素的attr和prop方法的理解

    一.背景 在编写使用高版本[ jQuery 1.6 开始新增了一个方法 prop()]的jquery插件进行编写js代码的时候,经常不知道dom元素的attr和prop方法到底有什么区别?各自有什么应 ...

  4. iOS应用架构谈(三):网络层设计方案(上)

    iOS客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来讨论iOS应用架构中的种种问题,本文是其中的第三篇,主要讲网络层设计以及安全机制和优化方案. 前言 网络层在一个Ap ...

  5. September 2nd 2016 Week 36th Friday

    How does the world look through your eyes? 你眼里的世界是什么样子的? How does the world look through your eyes? ...

  6. 如何解决exe4j生成exe文件后弹出提示信息

    使用exe4j生成exe文件时会提示以上一段信息,这个主要是没有注册导致的,在welcome to exe4j的右下角有一个注册信息的地方,去找个注册码,就OK了. 通用注册码:L-g782dn2d- ...

  7. struts2中一些常用的写法 记录

    1.对日期进行处理 Date current = new Date(); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ...

  8. PHP面向对象——重写与重载

    重写/覆盖           override 指:子类重写了父类的同名方法 class Human{    public function say($name){           echo $ ...

  9. DLog的使用

    DLog本质上就是个宏替换.DLog具体代码如下: #ifdef DEBUG #define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt) ...

  10. Android -- View setScale, setTranslation 对View矩阵的处理

    参考: 1.Android Matrix理论与应用详解 2.2D平面中关于矩阵(Matrix)跟图形变换的讲解 3.Android中关于矩阵(Matrix)前乘后乘的一些认识 4.Android Ma ...