UVALive - 3942 左儿子trie DP
题意:白书P209
本题用普通字典树会更快,为了练习还是尝试再敲一遍左儿子-右兄弟字典树(其实就是字典树上开前向星)
dp[i]为满足[i...len)的分配方案数,转移方程为dp[i]=sum{dp[i+slen(j)],如果后缀j存在符合前缀的方案,slen(j)为该后缀完全匹配前缀的长度}
这种利用前缀进行dp从后往前推的方法我并没有用过,学习了
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int MAXN = 2e6+11;
const int MOD = 20071027;
char str[MAXN>>1],s[MAXN>>1];
int dp[MAXN>>1];
struct Trie{
int son[MAXN],bro[MAXN],tot,root;
char val[MAXN];
int sz[MAXN];
int isword[MAXN];
void init(){
memset(son,-1,sizeof son);
root=0;tot=1;
val[root]=0;sz[root]=0;isword[root]=0;
}
int node(int fa,char ch){
bro[tot]=son[fa]; son[fa]=tot;//yuanlaide diyige zuoerzi bianwei youxiongdi
val[tot]=ch; sz[tot]=0; isword[tot]=0; // son[tot]=-1;
return tot++;
}
void insert(char str[]){
int now=0,lc=-1;
for(int i=0;str[i];i++){
bool flag=0;
for(lc=son[now];~lc;lc=bro[lc]){
if(val[lc]==str[i]){
flag=1;break;
}
}
if(!flag) lc=node(now,str[i]);
now=lc;sz[now]++;
}
isword[now]++;
}
int find(int from,int to){
int now=0,lc=-1,ans=0;
for(int i=from;i<to;i++){
bool flag=0;
for(lc=son[now];~lc;lc=bro[lc]){
if(val[lc]==str[i]){
flag=1;
if(isword[lc]) ans=(ans+dp[i+1])%MOD;
now=lc;
break;
}
}
if(!flag) break;
}
return ans;
}
}trie;
int main(){
int kase=0;
while(~scanf("%s",str)){
trie.init();
int n; scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s);
trie.insert(s);
}
int len=strlen(str);
memset(dp,0,sizeof dp);
dp[len]=1;//!!!
for(int i=len-1;i>=0;i--){
int tmp=trie.find(i,len);
dp[i]=tmp;
}
printf("Case %d: %d\n",++kase,dp[0]);
}
return 0;
}
UVALive - 3942 左儿子trie DP的更多相关文章
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- Vijos p1518 河流 转二叉树左儿子又兄弟
左儿子又兄弟的转发一定要掌握啊,竞赛必用,主要是降低编程复杂度,省时间.个人觉得状压DP也是为了降低编程复杂度. 方程就不说了,程序应该能看得懂,用的记忆化搜索,方便理解. #include<c ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- Remember the Word UVALive - 3942(dp+trie)
题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...
随机推荐
- linux下方便的录屏命令
linux下方便的录屏命令 ffmpeg -f x11grab -s 1024*768 -r 20 -i :0.0 -sameq ~/recode.mpg -r后是刷新屏率, 推出直接Ctrl ...
- 模板模式和Comparable类
模板模式中,父类规定好了一些算法的流程,并且空出一些步骤(方法)留给子类填充 Java的数组类中静态方法sort()就是一个模板,它空出了一个compareTo的方法,留给子类填充,用来规定什么是大于 ...
- 12.Alias(别名)
通过使用 SQL,可以为列名称和表名称指定别名(Alias). SQL Alias 表的 SQL Alias 语法 SELECT column_name(s) FROM table_name AS a ...
- 7.ORDER BY 子句
ORDER BY 语句 ORDER BY 语句用于根据指定的列对结果集进行排序. ORDER BY 语句默认按照升序对记录进行排序. 如果您希望按照降序对记录进行排序,可以使用 DESC 关键字. 1 ...
- .net对Cookie的简单操作
1 声明:HttpCookie MyCookie= new HttpCookie("test"); 2增加:MyCookie.Values.Add("key1" ...
- Robot Framework 使用总结
最近项目上使用了RF快速实现了一些验收测试的自动化case,感觉不错,很好用,下面就记录一下使用RF实现自动化的过程. 什么是RF? RF是一种测试框架,帮助测试人员在其框架下快速实现验收测试的自动化 ...
- c# 如何筛选datatable
对DataTable数据各种筛选 筛选一个DataTable的数据,赋值给另外一个DataTable 假设: 有2个DataTable:DataTable A.DataTable B. 要求: 筛选D ...
- form表单以get方式提交时action中?后面的参数部分不生效
form表单的提交方式是get方式,action="?sss=test",问号后面参数是接受不到的,谨记!
- Ubuntu 14.04.3 LTS如何安装谷歌输入法
谷歌输入法项目主页:https://code.google.com/p/scim-googlepinyin/ 一,打开Ubuntu 的“Ubunru的软件中心”,在搜索里面输入“googlepinyi ...
- 《javascript 高级程序设计》 笔记1 1~7章
chapter 2 在html中使用JavaScript chapter 3 基本概念 EMCAscript 语法 变量,函数名和操作符都区分大小写. 使用var定义的变量将成为定义该变量的作用域中的 ...