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为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...
随机推荐
- Servlet和JSP简述
什么是Servlet和JSP 用Java开发Web应用程序时用到的技术主要有两种,即Servlet和JSP. Servlet是在服务器端执行的Java程序,一个被称为Servlet容器的程序(其实就是 ...
- Java——操作Excel表格,读取表格内容
JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...
- git 的使用方法
git 的使用有3个主要步骤: 1.1 工作区域操作: 在自己的git账号下构建一个工作目录, 并往工作目录里添加文件内容(cp /root/data/VIP_Amount_prediction/* ...
- 997D Cycles in product
传送门 题目大意 https://www.luogu.org/problemnew/show/CF997D 分析 我们发现两棵树互不相关 于是我们可以分别求出两棵树的信息 我们点分,人啊按后设f[i] ...
- 第十三课 Actionlib(2)
上节课讲到了客户端,这节课讲解一下服务器 1.创建服务器源文件touch fibonacciserver.cpp 2.编写源文件 3.修改CMakeLists.txt 4.编译之catkin_make ...
- 第八课 ROS的空间描述和变换
1.tf的实际应用 1)在机器人的配置中 从上面可以看出激光雷达中心距离机器人底座的中心有20cm,激光雷达的中心距机器人底座中心有10cm,如果激光雷达在障碍物前面0.3米,那么机器人底座离障碍物多 ...
- Part10-C语言环境初始化-栈初始化lesson1
1.概念解析 ARM系统使用的是满栈! ARM采用降栈!!! 栈帧 每一个进程会有一个栈,该进程中的每一个函数会分割栈的一部分,那么每一个函数使用的那部分栈就叫做栈帧.那么所有栈帧组成了整个栈. 子函 ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- My First JS Page
哗啦啦~我的处女作终于浮出水面了^ ^值得高兴一下,虽然参考了人家的代码.给我的感觉JS就是用来实现动态网页的,比如说弹出一个框框,然后调用JS,返回些东西. 1.打开新写好的页面a.html,弹出了 ...
- 类操作,removeClass&addClass
// 添加类 function addClass(node,className){ var reg=new RegExp("\\b"+classNa ...