每个小单词的长度都是小于等于100的,这是个重要的突破口.

Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 300006
#define mod 20071027
using namespace std;
char str[maxn],wd[maxn];
int tag[maxn],n,f[maxn],m,cas;
struct TRIE{
#define SIGMA 30
int root,ch[maxn][SIGMA],cnt;
void init() {
root = cnt = 0;
memset(ch,0,sizeof(ch));
memset(tag,0,sizeof(tag));
memset(f,0,sizeof(f));
}
void ins(char s[]) {
int len = strlen(s),p = root;
for(int i = 0;i < len; ++i) {
if(!ch[p][s[i]-'a']) ch[p][s[i]-'a'] = ++cnt;
p = ch[p][s[i]-'a'];
}
tag[p] = 1;
}
void solve(int st){
int p = root;
for(int i = st;i <= m; ++i) {
p = ch[p][str[i]-'a'];
if(!p) break;
if(tag[p]) f[st] += f[i + 1],f[st] %= mod;
if(tag[p] && i == m) f[st] += 1;
}
}
}trie;
int main(){
//setIO("input");
while(scanf("%s",str) != EOF) {
scanf("%d",&n),trie.init(),m = strlen(str) - 1;
for(int i = 1;i <= n; ++i) scanf("%s",wd),trie.ins(wd);
for(int i = m;i >= 0; --i) trie.solve(i);
printf("Case %d: %d\n",++cas,f[0]);
}
return 0;
}

  

Remember the Word UVALive - 3942 DP_字典树的更多相关文章

  1. UVALive - 3942 (字典树)

    递推:$d(i) $表示从第$i$个字符开始到末尾的字符串(即后缀S[i...n])的分解方案数,则$d(i) = \sum {d(i + len(x))} $,其中字符串$x$代表S[i...n]的 ...

  2. Remember the Word UVALive - 3942(dp+trie)

    题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...

  3. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

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

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

  5. UVALive 3942 Remember the Word(字典树+DP)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

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

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

  8. UVALive 3942 字典树+dp

    其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...

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

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

随机推荐

  1. vue中slot的用法案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. hdu 5375 Gray code 【 dp 】

    dp[i][j]表示第i位取j的时候取得的最大的分数 然后分s[i]是不是问号,s[i-1]是不是问号这大的四种情况讨论 #include<cstdio> #include<cstr ...

  3. C/C++函数调用约定与this指针

    关于 C/C++ 函数调用约定,大多数时候并不会影响程序逻辑,但遇到跨语言编程时,了解一下还是有好处的. VC 中默认调用是 __cdecl 方式,Windows API 使用 __stdcall 调 ...

  4. LayUI中实现上级下拉框动态加载下级下拉框js

    js代码: var form = layui.form, layer = layui.layer; form.on("select(上级)", function(data){ va ...

  5. 利用UncaughtExceptionHandler捕获未try...catch到的异常

    public class test { public static void main(String[] args){ Thread thread = new Thread(new MyThread( ...

  6. 进程线程之pid,tid

    Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型pthread_t,由pthread_self()取得,该id由线程维护,其i ...

  7. HDU1420 - Prepared for New Acmer

    集训进行了将近2个礼拜,这段时间以恢复性训练为主,我一直在密切关注大家的训练情况,目前为止,对大家的表现相当满意,首先是绝大部分队员的训练积极性很高,其次,都很遵守集训纪律,最后,老队员也起到了很好的 ...

  8. java实现多个数字求和_图形化界面

    一,设计思想 1,通过简单的窗口实现多个数字的输入与输出. 2,可通过用户输入数字的数量来实现多个数字的求和. 3,定义整型数组变量number和字符串型数组变量integer,将输入的字符串变量赋给 ...

  9. 刷新页面vuex数据不消失和不跳转页面

    先说点什么 vuex和路由拦截这一块捣鼓的有一段时间了,总算是爬出来了,特地来分享一下,首先声明没有什么基础介绍,用的是登录状态存储sessionStorage的方法!!! 进入正题 刷新 刷新相当与 ...

  10. vue父组件引用子组件方法显示undefined问题原因及解决方法

    关于vue父组件引用子组件问题 1.首先导入子组件并且在components中定义子组件 2.引用子组件,并定义ref,ref定义的名称用于 this.$refs所调用的名称 3.调用子组件的方法 ( ...