Remember the Word

【题目链接】Remember the Word

【题目类型】递推+Trie

&题解:

蓝书P209,参考的别人公开代码

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn= 4005*100 +7,sigmaSize=26;
int ch[maxn][sigmaSize],sz,K;
bool vis[maxn];
void init() {sz=1; memset(ch[0],0,sizeof(ch[0]));}
void add(char *s) {
int u=0;
for(int i=0; s[i]; i++) {
int c=s[i]-'a';
if(!ch[u][c]) {
memset(ch[sz],0,sizeof(ch[sz]));
vis[sz]=false;
ch[u][c]=sz++;
}
u=ch[u][c];
}
vis[u]=true;
}
const int LEN=3e5+5,mod=20071027;
char str[LEN];
int d[LEN];
int main() {
freopen("E:1.in","r",stdin);
char word[120];
while(~scanf("%s",str)) {
init();
int S; scanf("%d",&S);
while(S--) {
scanf("%s",word);
add(word);
}
int n=0;
while(str[n]) d[n++]=-1;
d[n]=1;
for(int cur=n-1; cur>=0; cur--) {
int u=0;
d[cur]=0;
for(int i=cur; i<n; i++) {
int c=str[i]-'a';
if(!ch[u][c]) break;
u=ch[u][c];
if(vis[u]) d[cur]=(d[cur]+d[i+1])%mod;
}
}
printf("Case %d: %d\n",++K,d[0]);
}
return 0;
}

UVAL 3942 Remember the Word(递推+Trie)的更多相关文章

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

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

  2. Trie + DP LA 3942 Remember the Word

    题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...

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

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

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

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

  5. UVA 1401 Remember the Word(用Trie加速动态规划)

    Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...

  6. 第46套题【STL】【贪心】【递推】【BFS 图】

    已经有四套题没有写博客了.今天改的比较快,就有时间写.今天这套题是用的图片的形式,传上来不好看,就自己描述吧. 第一题:单词分类 题目大意:有n个单词(n<=10000),如果两个单词中每个字母 ...

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

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

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

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

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

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

随机推荐

  1. [No0000141]Outlook,设置全局已读回执

    Outlook,设置全局已读回执 文件->选项

  2. Mybatis 不同使用方式

    前言 工作这么多年,ORM框架一直选择Mybatis框架. Mybatis的使用方式也一直在变,总体来说是越来越简单.写篇文章对各使用方式做个总结... 正文 一.Mybatis典型用法 1. 正常执 ...

  3. Vue的自动化测试

    前言 为什么我们需要测试? 让产品可以快速迭代,同时还能保持高质量 -- 阮一峰 持续集成是什么? 对于一些相对稳定的系统级别页面,自动化测试在提高测试的效率的方面起到非常重要的作用.前端的自动化测试 ...

  4. [dpdk][kni] dpdk kernel network interface

    文档:https://doc.dpdk.org/guides/prog_guide/kernel_nic_interface.html 摘要: The KNI kernel loadable modu ...

  5. SYSAUX表空间如何清理

    ############################################################ 操作方案################################### ...

  6. omitting directory何意

    使用cp命令拷贝目录时,若该目录下有子目录 需加参数 cp -r 加目标目录到路径下

  7. js中的事件轮询(event loop)机制

    异步任务指的是,不进入主线程.而进入"任务队列"(task queue)的任务,只有"任务队列"通知主线程,某个异步任务可以执行了,该任务才会进入主线程执行. ...

  8. scala-LinkedList

    LinkedList每隔元素乘以3: import scala.collection.mutable.LinkedList var list1=LinkedList.apply(1,2,3,4,5) ...

  9. GDB常用命令系列

    本文由霸气的菠萝原创,转载请注明出处:http://www.cnblogs.com/xsln/p/gdb_instructions.html 本文为索引,请点击以下链接进行阅读: GDB调试原理——p ...

  10. 安全需求-建模归类——By Me

    漏洞与Bug并不等同,他们之间的关系基本可以描述为:大部分的Bug影响功能性,并不涉及安全性,也就不构成漏洞:大部分的漏洞来源于Bug,但并不是全部,它们之间只是有一个很大的交集.可以用如下这个图来展 ...