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) ...
随机推荐
- React Router 用法
React Router 用法 一.DEMO import React from "react"; import { HashRouter as Router, Route, Li ...
- 编译openssl和Apache报错checking for SSL_CTX_new... no
执行export LDFLAGS=-ldl命令后重新编译
- arcengine新建要素类
ArcGIS里面新建数据集,看起来简单,平时都是默认创建,实际上好多细节问题我们都没注意到 一.在数据集上新建要素类: How to create a feature class within a f ...
- regular expression (如何用Sed和正则表达式提取子字符串)
echo "mv u3_yunying.war u3_yunying.war_`date +%Y%m%d%H%M`" | sed "s/\(mv\) \(\w*.wa ...
- day 0313函数的初识
1.函数的定义: 定义:def 关键词开头,空格之后接函数名和圆括号(),还有最后一个‘:’ def是固定的,定义函数的关键字. 空格-是为了将关键字和函数名分开,必须有的. 函数名:只能包括字符串, ...
- springboot的filter使用
package com.filter; import org.springframework.core.annotation.Order; import javax.servlet.*; import ...
- shiro默认过滤器
- P1879 [USACO06NOV]玉米田Corn Fields 状压dp/插头dp
正解:状压dp/插头dp 解题报告: 链接! ……我真的太菜了……我以为一个小时前要搞完的题目调错误调了一个小时……90分到100我差不多搞了一个小时…… 然后这题还是做过的……就很气,觉得确实是要搞 ...
- SSH端口转发详解及实例-转载
作者:珂儿吖 出处:http://www.cnblogs.com/keerya/ 目录 1.1 SSH端口转发的两大功能 实验一:实现SSH端口转发——本地转发 实验二.实现SSH端口转发——远程转发 ...
- JSON.parseObject 和 JSON.toJSONString
JSON.parseObject,是将Json字符串转化为相应的对象:JSON.toJSONString则是将对象转化为Json字符串.在前后台的传输过程中,Json字符串是相当常用的,这里就不多介绍 ...