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为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...
随机推荐
- 1、 Shiro框架:认证,授权(验权 2. Shiro框架实现权限控制方式:
1. Shiro框架:认证,授权(验权) a) 认证逻辑:applicationCode—>通过工具类获取subject对象,调用login方法参数令牌信息->安全管理器------> ...
- 僵固式思维 OR 成长式思维
有意无意中,看到这样的一篇文章,觉得非常富有正能量,而且也比较有同感.而且,不仅仅对于职场暂时失落或者失意的人有帮助,就是对学生,也一样的.故特分享,以共勉之. 我想每个新人进入职场之后都会遇到的第一 ...
- Python基础-3
目录 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 知识插入:嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 一.函数基本语法 函数是什么? 函数一词 ...
- C#获取文件的Content-Type(MIME Type)的方法
使用静态类MimeMapping(需要.NET Framework 4.5及以后的支持) string fileName = "D:\myfile.txt"; var conten ...
- css总结12:CSS Float(浮动)
1 定义:CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列. 2 解释:浮动的元素,浮在界面上,脱离了HTML文本流. 元素的水平方向浮动,意味着元素只能左右移动而不能 ...
- HDU 4803 Poor Warehouse Keeper(贪心)
题目链接 题意 :屏幕可以显示两个值,一个是数量x,一个是总价y.有两种操作,一种是加一次总价,变成x,1+y:一种是加一个数量,这要的话总价也会相应加上一个的价钱,变成x+1,y+y/x.总价显示的 ...
- 20169219 实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 一.程序设计中临时变量的使用 任务: //定义一个数组 int arr[] = {1,2,3,4,5,6,7,8}; //打印原始数组的值 for(int i:arr ...
- 关于Java异常一段很有意思的代码
今天学习了Java的异常,讲到try-catch-finally时,老师演示了一段代码,觉得很有意思,很能反映出其执行的过程,让自己有点绕,特意记录一下. 只要代码执行到try代码内部, 不管有没有异 ...
- centos 重新安装python3.6之后 yum 无法使用报错
问题: $ yum File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid ...
- IOS 防坑指南
1. 读写文件 1. IOS 8 中 stringWithContentsOfFile 已被移除 2. 创建文件必须放到 应用下 Documents 下面 // // FileHelper.swif ...