UVAL 3942 Remember the Word(递推+Trie)
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)的更多相关文章
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- Trie + DP LA 3942 Remember the Word
题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- UVA 1401 Remember the Word(用Trie加速动态规划)
Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...
- 第46套题【STL】【贪心】【递推】【BFS 图】
已经有四套题没有写博客了.今天改的比较快,就有时间写.今天这套题是用的图片的形式,传上来不好看,就自己描述吧. 第一题:单词分类 题目大意:有n个单词(n<=10000),如果两个单词中每个字母 ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- LA 3942 Remember the Word(前缀树&树上DP)
3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
随机推荐
- 启用hive hwi方法
hive启动hwi: ./hive --service hwi ls: cannot access /opt/cdh-5.3.6/hive-0.13.1/lib/hive-hwi-*.war: No ...
- n个元素的数组向左循环移动i个位置
算法的完美 时间:2012-03-19 / 分类:学习园地,网络文摘 / 浏览:1797 / 0个评论 发表评论 考虑一个问题:将一个具有n个元素的数组向左循环移动i个位置.有许多应用程序会调用这个问 ...
- Xcode 编辑器之Workspace,Project,Scheme,Target
一,前言 最近老是突然对Workspace,Project,Scheme,Target四者的关系有些疑惑,所以查阅资料总结一下. 二,Workspace,Project,Scheme,Target四者 ...
- LeetCode 463 Island Perimeter 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- linux查找大文件命令
测试服务器用久了,如果没有运行自动清除日志的脚本,会导致硬盘空间不足,应用.数据库.环境等启动不了: 如果你对系统不是特别熟悉,就无法知道那些占用空间的日志或缓存文件在哪里,这时,我们就可以利用查找大 ...
- linux测试环境搭建步骤
一.建用户 1.新建用户root用户登录,执行命令:useradd + 用户名 -m -d + 指定路径如:新建用户liuwq ,指定路径/home/ios命令:useradd liuwq -m -d ...
- VSCode代码修改后跑起来没反应,打开本地文件,代码没变化
两种解决办法: 首先:修改VSCode默认配置文件,点击左下角设置标志图 -> 设置,出来了设置相关的东西,搜索 files.autoSave 第一种:把"files.autoSave ...
- swift中 ?和 !的区别
可选类型(?)与强制解析运算符(!) ?是一种判断后再拆包的语法糖 !是一种强制拆包的语法糖 当你不确定有值的时候就可以用 ? 当你确定有值的时候可以用 ! ?的几种使用场景:1. ...
- 接口测试工具-Jmeter使用笔记(二:GET/POST请求参数填写)
举例来说 我的被测系统API的http请求涉及到GET/POST/PUT/DELETE四种.请求传参可分为两种: GET请求 http://请求路径/Ecs-duHc0U4E #该请求参数“Ecs-d ...
- SVN安装部署
svn安装版本用的是1.8 SVN属于功能性软件,yum安装即是最佳实践. 安装svn yum install subversion 检查svn是否安装完毕 [root@mysql ~]# rpm - ...