每个小单词的长度都是小于等于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. 实现简单的List功能

    简单的实现javaArrayList(可扩容)功能,实现新增,删除,取数据. package algorithm.data_structure; /** * 模拟ArrayList类的功能 * @au ...

  2. MySQL 5.6.26 通过frm & ibd 恢复数据过程

    在A服务器上创建数据库yoonroot(yoon)> show create table yoon\G*************************** 1. row *********** ...

  3. Primitive data types recap

  4. Excel基础视频教程在线观看

    也许你已经在Excel中完成过上百张财务报表,也许你已利用Excel函数实现过上千次的复杂运算,也许你认为Excel也不过如此,甚至了无新意.但我们平日里无数次重复的得心应手的使用方法只不过是Exce ...

  5. luogu P2000 拯救世界 生成函数_麦克劳林展开_python

    模板题. 将所有的多项式按等比数列求和公式将生成函数压缩,相乘后麦克劳林展开即可. Code: n=int(input()) print((n+1)*(n+2)*(n+3)*(n+4)//24)

  6. faker smtp server

    import os import asyncio import logging import base64 from email import message_from_bytes from emai ...

  7. npx命令

    npx命令 查了一下, 英文资料: https://www.npmjs.com/package/npx 中文资料: 什么是npx 第一次看到npx命令是在 babel 的文档里 Note: If yo ...

  8. [读书笔记] R语言实战 (三) 图形初阶

    创建图形,保存图形,修改特征:标题,坐标轴,标签,颜色,线条,符号,文本标注. 1. 一个简单的例子 #输出到图形到pdf文件 pdf("mygrapg.pdf") attach( ...

  9. java实例化对象的五种方法

    1.用new语句创建对象,这是最常见的创建对象的方法. 2.通过工厂方法返回对象,如:String str = String.valueOf(23); 3.运用反射手段,调用java.lang.Cla ...

  10. 学一下HDFS,很不错(大数据技术原理及应用)

    http://study.163.com/course/courseMain.htm?courseId=1002887002 里面的HDFS这一部分.