题意:白书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的更多相关文章

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

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

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

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

  3. UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)

    题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...

  4. 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?

    题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...

  5. UVA 3942 Remember the Word (Trie+DP)题解

    思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...

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

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

  7. Vijos p1518 河流 转二叉树左儿子又兄弟

    左儿子又兄弟的转发一定要掌握啊,竞赛必用,主要是降低编程复杂度,省时间.个人觉得状压DP也是为了降低编程复杂度. 方程就不说了,程序应该能看得懂,用的记忆化搜索,方便理解. #include<c ...

  8. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  9. Remember the Word UVALive - 3942(dp+trie)

    题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...

随机推荐

  1. Docker学习笔记_使用Dockerfile创建flask的一个镜像

    一.实验环境 1.宿主机OS:Win10 64位 2 .虚拟机OS:Ubuntu18.04 64位    虚拟机名称:Ubuntu18VM1   虚拟机IP:192.168.8.25 3.账号:doc ...

  2. 下载Redis

    1.下载当前Redis 官网:https://redis.io/ 当前稳定版本是4.0.11,如下图,点Download it下面的链接进行下载 2.下载历史版本的Resis 网址: http://d ...

  3. tensor 维度 问题。

    tf.argmax takes two arguments: input and dimension. example: tf.argmx(arr, dimension = 1). or tf.arg ...

  4. hdu 4741 Save Labman No.004 (异面直线的距离)

    转载学习: #include <cstdio> #include <cstdlib> #include <cstring> #include <algorit ...

  5. css总结13:CSS 伪类(Pseudo-classes)

    1 伪类作用:CSS伪类是用来添加一些选择器的特殊效果. 2 常用示例: 2.1anchor伪类:代码:   正常语法: a{color:#FF0000;}/* 文字颜色 */   伪类语法: a:l ...

  6. SQL之DML

    DML(Data Manipulation Language)数据操纵语言statements are used for managing data within schema objects. 由D ...

  7. Sql Server - CURSOR (游标)

    1.声明游标            DECLARE 游标名 CURSOR SELECT语句(注:此处一定是SELECT语句)        2.打开游标           OPEN 游标名 3.读取 ...

  8. IE11下使用fixed定位时鼠标滚动不平滑

    很久不用IE了,近期做兼容性测试发现一个fixed定位的问题,当元素使用fixed定位时,其应该不随页面滚动,在chrome/firefox/edge下都很完美,元素完全不动,但是使用IE11时,如果 ...

  9. mvc - codefirst 数据迁移

    from :http://blog.csdn.net/xiaoyiyz/article/details/41485325

  10. C# 调接口

    上一个项目,需要mvc管理后台调接口项目,以便后期的重构扩展,调研后发现后台用的ajax请求,直接调接口可能会有跨域问题,最终在c#代码中实现了这个需求. 1,Ajax请求后台 将接口所需参数传入 2 ...