今天继续攒人品。。。真开心啊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串的更多相关文章

  1. UVALive - 6577 Binary Tree 递推+找规律

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48421 Binary Tree Time Limit: 3000MS 问题描述 Binary Tree is ...

  2. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  3. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  4. [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 ...

  5. [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 ...

  6. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. nginx禁止ip直接访问

    编辑一个noIp.conf放到虚拟目录中 server { listen default; server_name _; rewrite ^ http://www.xxxx.com/; } 其中 ww ...

  2. hdu3729 I'm Telling the Truth (二分图的最大匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS (Java/ ...

  3. oracle中时间运算

    Oracle两个函数相减,默认得到的是天数,按日期格式,精准到响应的精度,如用sysdate(2015/12/7 10:17:52),时间差精确到秒. 在此基础上,oracle两个时间相减默认天数*2 ...

  4. HTML5+学习笔记2-------边看代码边研究貌似还是有点问题...还在研究中api中

    // 拍照 function getImage() { outSet( "开始拍照:" ); var cmr = plus.camera.getCamera(); cmr.capt ...

  5. C# Winform 脱离 Framework (二)

    第一个Method: //启动应用程序 VOID RunApplication(LPTSTR lpFilename, LPTSTR args) { //WinExec(lpFilename, SW_S ...

  6. [设计模式] javascript 之 责任链模式

    责任链模式:定义 责任链接模式又称职责链模式,是一种对象的行为模式:它是一种链式结构,每个节点都有可能两种操作,要么处理该请求停止该请求操作,要么把请求转发到下一个节点,让下一个节点来处理请求:该模式 ...

  7. 黑客攻防技术宝典Web实战篇(二)工具篇DVWA Web漏洞学习

    DVWA是一个学习Web漏洞的很好的工具. DVWA全程是Damn Vulnerable Web Application,还有一个跟它一样好的工具尽在http://www.360doc.com/con ...

  8. HDOJ 2444 The Accomodation of Students

    染色判读二分图+Hungary匹配 The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limi ...

  9. JQuery测手速小游戏-遁地龙卷风

    (-1)写在前面 我用的chrome49,jquery3.0,我得到过399分,信不信由你. (1)设计思路 两个p元素放在div里,每个p元素的高度和宽度都和div一样,当鼠标放在div上时,第一个 ...

  10. ubuntu安装android开发环境

    1.安装oracle-jdk 打开终端,使用下面的命令: java -version 如果你看到像下面的输出,这就意味着你并没有安装过Java: The program ‘java’ can be f ...