UVALive - 6577 Binary Tree 递推+找规律
题目链接:
http://acm.hust.edu.cn/vjudge/problem/48421
Binary Tree
Time Limit: 3000MS
#### 问题描述
> Binary Tree is a tree data structure where each node has at most two children, usually they are
> distinguished as left and right child. And the node having the children are called parent of those
> children.
> An instruction string is a string consisting of the letters L, R and U. L stands for Left, R for Right
> and U for Up. Meaning of these will be clear shortly.
> One day I have drawn an infinitely large Binary Tree. In this tree each node has exactly two
> children (left child and right child) and each of them has a parent. For this problem, we will consider
> the parent of the root is root itself. I put a pen in the root and follow the instruction string S.
> That is, we look at the first character if it says L we go to left child, if it says R we go to right child
> and if it says U then to the parent. If we receive a U instruction at root we just end up at root since we
> assumed parent of the root is root itself.
> Now we have another instruction string T. Starting from the node where we are after following the
> instruction string S, we will follow the instruction string T. But this time, if we wish we may skip any
> instruction in the string T (possibly discarding all of them). You have to tell me how many different
> nodes I can end up after following instruction string T (skipping as many instructions as I wish).
> For example:
> Suppose: S = L and T = LU. Our answer is 3. Following S we will end up at the left child of the
> root. Now, when we follow T, there may be 4 cases:
> i Skipping all letters: we will be at the same node where we are.
> ii Skipping L and following U: we will be at the root.
> iii Following L and skipping U: we will be at the left child of current node.
> iv Following both L and U: we will be at the same node as in case i.
> Since 3 different nodes we can end up after following T, the answer is 3.
输入
First line of the test file contains an integer N (≤ 20) denoting number of test cases. Hence follow N
test cases. Each test case consists of two non empty strings. First line will contain instruction string S
and the second line will contain the instruction string T. You may assume that there will not be any
letter other than L, R or U in these strings. Length of the strings will not be greater than 100000.
输出
For each test case print the case number followed by the number of nodes we can end up finally. Since
the answer may be large, you have to give the answer modulo 21092013.
样例
sample input
2
L
LU
L
Lsample output
Case 1: 3
Case 2: 2
题解
首先我们考虑只有LR,没有U的情况:
我们定义三个计数变量:ans=1,l=1,r=1;
其中ans为答案,l表示当前局势下,往左走一步能够访问到的新节点;r表示当前局势往右走一步能够访问到的新节点。
如果现在的输入是'L',那么显然会有ans+=l。并且不会影响r,且这些新节点还会创造出更多的能够往右走的新节点:r+=l。
那么,现在我们考虑有'U'的情况:
如果U没有上升到一个没有走过的点,显然是要亏一波的,还不如直接跳过。
因此我们只需要考虑往上走能够走到新节点的情况(用S串来获取信息):
如果父亲在左边(它是右儿子),那么将会有l+=1; ans+=1;
如果父亲在右边:那么有r+=1,ans+=1;
代码
#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define pb(v) push_back(v)
#define sz() size()
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const int maxn = 1e5+10;
const int mod=21092013;
char s1[maxn],s2[maxn];
vector<char> sta;
void add_mod(int &x,int y){ x=(x+y)%mod; }
void init(){
sta.clear();
}
int main() {
int tc,kase=0;
scanf("%d",&tc);
while(tc--){
init();
scanf("%s",s1);
int l1=strlen(s1);
rep(i,0,l1){
if(s1[i]=='U'){
if(sta.size()) sta.pop_back();
}else{
sta.pb(s1[i]);
}
}
scanf("%s",s2);
int l2=strlen(s2);
int l=1,r=1,ans=1;
rep(i,0,l2){
if(s2[i]=='U'){
if(sta.size()){
char last=sta[sta.sz()-1];
sta.pop_back();
if(last=='L'){
add_mod(r,1);
add_mod(ans,1);
}else{
add_mod(l,1);
add_mod(ans,1);
}
}
}else if(s2[i]=='L'){
add_mod(r,l);
add_mod(ans,l);
}else{
add_mod(l,r);
add_mod(ans,r);
}
}
printf("Case %d: %d\n",++kase,ans);
}
return 0;
}
UVALive - 6577 Binary Tree 递推+找规律的更多相关文章
- MT【103】二阶递推找规律
评:如果直接找$a_n$的二阶递推式:$a_{n+2}-2\sqrt{2}a_{n+1}-a_n=0$有根号,不利于估计尾数.
- Full Binary Tree(二叉树找规律)
Description In computer science, a binary tree is a tree data structure in which each node has at mo ...
- codeforces 353D 递推 找规律
题意:一组男生女生在排队,每秒钟所有排在女生左边的男生与她相邻的女生交换位置,求女生全部换到男生前面的时间. 思路: 解法一:队伍最前面的那些女生不需要交换,后面的女生有两种状态:畅通无阻,前一个女生 ...
- UVALive 6577 Binary Tree 二叉树的LRU串
今天继续攒人品...真开心啊O(∩_∩)O~~各种身体不舒服~~ https://icpcarchive.ecs.baylor.edu/external/65/6577.pdf 题意是这样的,现在有一 ...
- LA 3357 (递推 找规律) Pinary
n位不含前导零不含连续1的数共有fib(n)个,fib(n)为斐波那契数列. 所以可以预处理一下fib的前缀和,查找一下第n个数是k位数,然后再递归计算它是第k位数里的多少位. 举个例子,比如说要找第 ...
- 51nod 1350 斐波那契表示(递推+找规律)
传送门 题意 分析 我们发现该数列遵循下列规律: 1 1,2 1,2,2 1,2,2,2,3 1,2,2,2,3,2,3,3 我们令A[i]表示f[i]开始长为f[i-1]的i的最短表示和 那么得到A ...
- POJ 2499 Binary Tree(二叉树,找规律)
题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向 ...
- C. Tennis Championship dp递推 || 找规律
http://codeforces.com/contest/735/problem/C C. Tennis Championship time limit per test 2 seconds mem ...
- "红色病毒"问题 HDU 2065 递推+找循环节
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=2065 递推类题目, 可以考虑用数学方法来做, 但是明显也可以有递推思维来理解. 递推的话基本就是状态 ...
随机推荐
- silverlight 不能输入中文问题
<param name="Windowless" value="true" />将调用silverlight页面的这句删除掉应该就能解决问题了 1. ...
- css下拉菜单效果
<style> *{padding: 0; margin: 0;} .menu {} li { list-style-type: none; } .menu li {float: left ...
- java实现的MySQL自动备份和还原(struts2+Hibernate)---兼容 window+Linux
相信很多朋友都经历过数据库出问题的情况,我也同样(见我的上一篇博文:phpmyadmin误删表后的恢复过程(心惊胆跳啊) ).如果数据很大或者很重要,那么恢复起来是相当困难的,所以我们在做一个相对 ...
- 合并果子 (codevs 1063) 题解
[问题描述] 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和 ...
- 重定向语句Response.Redirect()方法与Response.RedirectPermanent()对搜索引擎页面排名的影响
在ASP.NET中,开发人员经常使用Response.Redirect()方法,用编程的手法,将对老的URL的请求转到新的URL上.但许多开发人员没有意识到的是,Response.Redirect() ...
- SPARK 数据统计程序性能优化。
昨天写完R脚本 没测试就发到博客里, 结果实际运行发现很慢,运行时间在2小时以上, 查看spark控制台, 大量时间消耗在count上, 产生的stage多大70多个 . 分析原因. 1 selec ...
- Learning Scrapy笔记(六)- Scrapy处理JSON API和AJAX页面
摘要:介绍了使用Scrapy处理JSON API和AJAX页面的方法 有时候,你会发现你要爬取的页面并不存在HTML源码,譬如,在浏览器打开http://localhost:9312/static/, ...
- DrawTool多重笔之前奏 => 通过InkAnalyzer实现图形识别
这里要介绍的是通过InkAnalyzer来实现简单图形的识别,例如圆,椭圆,正方形,三角形等,当然你也可以通过扩展来实现自定义图形的识别,在使用InkAnalyzer前,你需要引用IAWinFX.dl ...
- VC下的人人对弈五子棋(dos)
#include"stdio.h"#include"stdlib.h"#include"string.h"#include "io ...
- Python数据类型-----数字&字符串
Python数字类型 int类型表示的范围:-2147483648至2147483648之间,超出这个范围的数字即视为long(长整形) 在Python中不需要事先声明数据类型,它是根据具体的赋值来进 ...