UVALive 6577 Binary Tree 二叉树的LRU串
今天继续攒人品。。。真开心啊O(∩_∩)O~~各种身体不舒服~~
https://icpcarchive.ecs.baylor.edu/external/65/6577.pdf
题意是这样的,现在有一个向下无限延伸的二叉树。然后输入起点(通过只含LRU的字符串S,从根结点开始执行)。LRU分别表示往左儿子走,往右儿子走,往爹娘处走(根结点的爹娘是自己,估计他是石头里蹦出来的)。
然后输入一个可选步骤串T。可以选择T中的子序,从起点开始走。然后问可以走到多少个不同的结点。
比赛的时候不会做啊╮(╯▽╰)╭。赛后好像有题解不过看不懂。。。。英语渣的缘故吧,我猜。。。然后看LC他们的代码,研究下终于搞懂的样子
我们可以先考虑,只有LR的情况,初始化,ans=1,L=1,R=1 。LR分别表示往左(右)走的新结点数量。然后遍历T字符串,然后如果有L则ans+=L,R+=L;其实就是往左走为往右走开辟了往右走的新结点。。。好别扭,不知道怎么解释。。建议画图模拟。。。然后如果有R则ans+=R,L+=R。。。。这个好像是做过的某一题
好了,只有LR的情况解决了=。=
然后如果是现在要up,如果是up到从根执行S串的路途中,那如果up到的结点最后一次往下走是left,那现在up上去必然的结果就是,开辟了一个往右的新结点,反过来是right也一样=。=同时答案+1
有可以参考的人(代码)真好啊~~~好像太依赖参考了。。。
复杂度就O(n)
以上都是在晕晕的状态写的=。=所以有那啥的求评论。。。
Note: 好像忘memset也AC。。。。还是不需要memset?
P.S.边吃饭边发现,那个dir是可以不用memset的~~~看来要多吃饭~~~
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <set>
using namespace std; #define ll long long
#define inf 0x3f3f3f3f
#define eps 1e-8
#define maxn 100010
#define mod 21092013 char a[maxn],b[maxn];
char dir[maxn];
int main(){
int t,ca=;
scanf("%d",&t);
while(t--){
scanf("%s%s",a,b);
//memset(dir,0,sizeof(dir));// 可以不用memset
int dep=;
int la=strlen(a),lb=strlen(b);
for(int i=;i<la;++i){
if(a[i]=='U')dep=max(dep-,);
else dir[dep++]=a[i];
}
int ans=,l=,r=;
for(int i=;i<lb;++i){
if(b[i]=='U'){
if(--dep<){
dep=;
continue;
}
ans=(ans+)%mod;
if(dir[dep]=='L')r=(r+)%mod;
else if(dir[dep]=='R')l=(l+)%mod;
}
else if(b[i]=='L'){
ans=(ans+l)%mod;
r=(r+l)%mod;
}
else if(b[i]=='R'){
ans=(ans+r)%mod;
l=(l+r)%mod;
}
}
printf("Case %d: %d\n",++ca,ans);
}
return ;
}
UVALive 6577 Binary Tree 二叉树的LRU串的更多相关文章
- UVALive - 6577 Binary Tree 递推+找规律
题目链接: http://acm.hust.edu.cn/vjudge/problem/48421 Binary Tree Time Limit: 3000MS 问题描述 Binary Tree is ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 543. Diameter of Binary Tree 二叉树的直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
随机推荐
- git log --stat常用命令
1,显示被修改文件的修改统计信息,添加或删除了多少行. git log --stat 2,显示最近两条的修改 git log --stat -2 3,显示具体的修改 git log -p -2 4, ...
- php补充
PHP 教程 echo 和 print 之间的差异:echo - 能够输出一个以上的字符串print - 只能输出一个字符串,并始终返回 1提示:echo 比 print 稍快,因为它不返回任何值. ...
- 第31天 mvp
interactor调用接口 Activity包含Presenter,这样界面上的操作就会通知到Presenter. Presenter调用view接口, Activity实现view接口,这样Pre ...
- MFC关闭子窗口 如何把父窗口也一起关闭
在子窗口OnClose() 消息里添加: GetParent()->DestroyWindow();//子窗口关闭 父窗口也同样关闭
- 如何让网页在浏览器标题栏显示自己制作的图标ico
第一步,制作一个尺寸16x16大小的PNG图片,可以用photoshop等图片处理工具来设计,然后保存到本地电脑上,通过ico在线制作或使用IconWorkshop工具制作ICO图标,ico图标命名为 ...
- ThinkPHP框架表单验证
对注册到test表的表单进行验证 在注册之前要对表单进行验证: 用户名非空验证,两次输入密码必须一致即相等验证,年龄在18~50之间即范围验证,邮箱格式正则验证. 自动验证是ThinkPHP模型层提供 ...
- Javascript高级程序设计——基本包装类型
既然js中的基本类型没有属性和方法那么为什么对字符串进行subString()方法可以呢?基本类型不应该没有方法的吗? 这就是基本包装类型啦! ECMAScript提供了三个特殊的引用类型,Boole ...
- 纯CSS多级菜单
主要代码部分: /*新增的二级菜单部分*/ .menu ul ul { visibility:hidden;/*开始时是隐藏的*/ position:absolute; left:0px; top:3 ...
- 淘宝(阿里百川)手机客户端开发日记第十二篇 mysql的认识
我这里用的是wamp,大家可以到网上去下载对应的包,自行安装,对于程序员来讲,安装软件大部分都应该不是问题的,所以我不去将具体安装的方法. wamp安装好后,在我们屏幕的右下角, 这样的图标,我们右键 ...
- css代码优化
一.CSS代码优化作用与意义 1.减少占用网页字节.在同等条件下缩短浏览器下载css代码时间,相当于加快网页打开速度2.便于维护.简化和标准化css代码让css代码减少,便于日后维护3.让自己写的cs ...