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 ...
随机推荐
- linux shell 报错 Syntax error: Bad for loop variable
在linux下写了一个简单的shell,循环10次. test.sh #!/bin/bash ## ##循环10次 ## ; i<; i++)); do echo Good Morning ,t ...
- CSS样式案例(2)-制作一个简单的登录界面
首先来张完工的效果图. 一.html文件如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- IE浏览器版本判断
<script type="text/javascript"> var browser=navigator.appName var b_version=navigato ...
- Hello 畅连·西瓜 帮助与更新
无感认证很好用,软件不再更新, 感谢每一位朋友的陪伴,谢谢! (2016.12.15) 百度云:点击下载 ------------旧版更新日志------------- Hello 畅连·西瓜 官网: ...
- Javascript高级程序设计——基本类型和引用类型的值
ECMAScript中的变量有两种不同的数据类型的值: 基本类型:基本类型的值是简单的数据段.包括:Undefined.Null.Number.Boolean.String五种 引用类型:引用类型的值 ...
- BZOJ2555——SubString
0.题目很短,就不概括了 给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. ...
- 忧桑三角形,调了半天,真忧桑TAT
忧桑三角形 试题描述 小J是一名文化课选手,他十分喜欢做题,尤其是裸题.有一棵树,树上每个点都有点权,现在有以下两个操作: 1. 修改某个点的点权 2. 查询点u和点v构成的简单路径上是否能选出三个点 ...
- jquery的change():下拉框值改变时触发
<script src="jquery.min.js" type="text/javascript"></script> <scr ...
- 一起入门python6之函数
今天我们来学习新的一篇吧,那便是“函数(function)”我们用def来定义一个函数,以案例说话.>>> def name(x): #定义一个“name”的函数. ...
- Ngrok搭建服务器
一.ngrok简介及作用 ngrok 是一款用go语言开发的开源软件,它是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道.下图简述了ngrok的原理. ngrok 可 ...