/**
题目:UVALive 3942 Remember the Word
链接:https://vjudge.net/problem/UVALive-3942
题意:给定一个字符串(长度最多3e5)和m个单词(每个单词长度最多100)。单词都是不同的。该字符串可以由若干个单词组成,问最多有多少种组合方式。 思路:字典树+dp
用字典树处理好m个单词,定义dp[i]表示从i开始的字符串可以由单词组成的方式数。
那么dp[i] += dp[i+j]; j表示某个单词和字符串的[i,i+j-1]匹配。 */ #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int mod = ;
const int INF = 0x3f3f3f3f;
const int maxnode = 4e5+;///最多可能有多少个节点
const int maxn = 3e5+;
const int sigma_size = ;///26个字母
int dp[maxn];
char s[maxn], ss[maxn];安全1
int ch[maxnode][sigma_size];///由于很大,所以结构体内部放不下。要放在外面。
struct Trie{
int val[maxnode];
int sz;
int idx(char c){return c-'a';} void insert(char *s,int v)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
} void query(int sta,int off)
{
int u = ;
int c = idx(s[sta]);
while(s[sta]!='\0'&&ch[u][c]){
if(val[ch[u][c]]){
dp[sta-off] = (dp[sta-off]+dp[sta+])%mod;
}
u = ch[u][c];
c = idx(s[++sta]);
off++;
}
}
}; int main()
{
int cas = ;
int n;
Trie trie;
while(scanf("%s",s)==)
{
scanf("%d",&n);
trie.sz = ;
memset(ch[], , sizeof ch[]);
for(int i = ; i < n; i++){
scanf("%s",ss);
trie.insert(ss,);
}
int m = strlen(s);
memset(dp, , sizeof dp);
dp[m] = ;
for(int i = m-; i>=; i--){
trie.query(i,);
}
printf("Case %d: %d\n",cas++,dp[]);
}
return ;
}

UVALive 3942 Remember the Word 字典树+dp的更多相关文章

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

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

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

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

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

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

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

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

  5. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  6. Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp

    C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...

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

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

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

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

  9. UVALive 3942 字典树+dp

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

随机推荐

  1. Linux环境下搭建MYSQL数据库指令详情

    一.mysql数据库的安装 确保安装gcc(开发工具) #groupadd mysql #useradd -g mysql mysql #cd /usr/local # tar -zxvf mysql ...

  2. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

  3. KRBTabControl

    This article explains how to make a custom Windows Tab Control in C#. Download demo project - 82.4 K ...

  4. Windows录音API学习笔记

    Windows录音API学习笔记 结构体和函数信息  结构体 WAVEINCAPS 该结构描述了一个波形音频输入设备的能力. typedef struct { WORD      wMid; 用于波形 ...

  5. OpenCV 4.1 编译和配置

    OpenCV 4.0 版本,历时3年半,终于在2018年圣诞节前发布了,该版本增加的新功能如下: 1) 更新代码支持 c++11 特性,需要兼容 c++11 语法的编译器 2)增加 dnn 中的模块功 ...

  6. 南阳OJ 背包问题

     /*背包问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 如今有非常多物品(它们是能够切割的),我们知道它们每一个物品的单位重量的价值v和 重量w(1< ...

  7. C8051F340之USB简介

    背景 需求为实现一个键盘产品,于是将C8051F340作为主控制器.本篇记录C8051F340的USB控制器相关知识点,阅读本篇文章需要读者具有最基本的USB相关知识. 正文 C8051F340结构 ...

  8. Android实现换肤功能(二)

    前两天写的上章关于换肤的功能获得了很好的反响,今天为大家介绍另一种方式.今天实现的策略也是网友建议的,然后我自己去写了个demo,大家自己评估下相比第一种方式的优势和劣势在哪里. 简单介绍下关于第一种 ...

  9. angular绑定数据 使用循环输出列表数据

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  10. 在Windows8系统下exe格式会计课件下载播放帮助图解

    近期非常多会计从业人员都開始购买课件,開始学习,准备考试:可是网校的课件有些是EXE扩展名格式的,在Windows8系统下播放比較困难.方法比較曲折,这里用图说话,给大家一点參考,希望对大家实用. 下 ...