atcoder: Moves on Binary Tree
先进行压缩move的次数,再用biginteger.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List; public class Main { public static Object[] compact(String S) {
LinkedList<String> sb = new LinkedList<>();
int unum = 0;
char lastch = 'A'; for (int len = S.length(), i = len - 1; i >= 0;i--) {
char ch = S.charAt(i); if (lastch == 'U') {
if (ch == 'U') {
unum++;
} else if (ch != 'U' && unum > 0) {
unum--;
if (unum == 0) {
sb.removeLast();
lastch = ch;
}
} else {
lastch = ch;
sb.addLast(""+ch);
}
} else {
if (ch == 'U') {
lastch = 'U';
unum = 1;
sb.add(""+ch);
} else {
lastch = ch;
sb.add(""+ch);
}
}
} return new Object[]{sb,unum};
} public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] words = reader.readLine().split(" ");
int n = Integer.valueOf(words[0]);
long x = Long.valueOf(words[1]);
String S = reader.readLine(); Object[] obj = compact(S);
LinkedList<String>list = (LinkedList<String>)obj[0];
int unum = (int) obj[1]; BigInteger ans = BigInteger.valueOf(x);
for (int i = 0,len=list.size();i<len;i++) {
char ch = list.pollLast().charAt(0);
switch (ch) {
case 'U':
ans = ans.shiftRight(unum);
break;
case 'R':
ans = ans.shiftLeft(1);
ans = ans.add(BigInteger.ONE);
break;
case 'L':
ans = ans.shiftLeft(1);
break;
default:
break;
}
}
System.out.println(ans); reader.close();
} }
atcoder: Moves on Binary Tree的更多相关文章
- [Swift]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree
Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there ar ...
- [atcoder contest 010] F - Tree Game
[atcoder contest 010] F - Tree Game Time limit : 2sec / Memory limit : 256MB Score : 1600 points Pro ...
- LeetCode 979. Distribute Coins in Binary Tree
原题链接在这里:https://leetcode.com/problems/distribute-coins-in-binary-tree/ 题目: Given the root of a binar ...
- LC 979. Distribute Coins in Binary Tree
Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there ar ...
- 【leetcode】979. Distribute Coins in Binary Tree
题目如下: Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and th ...
- 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- Cnpack ctrl+alt+v 来回切换 变量声明区,和代码写区,非常方便
Cnpack ctrl+alt+v 来回切换 变量声明区,和代码写区,非常方便 非常方便
- 【MFC学习一】BROWSEINFO选择路径导出文件
mfc中使用 BROWSEINFO,使用 bi.lpfn = BrowseCallbackProc; 回调指定默认当前程序所在目录,导出csv文件,注意处理文件内容中的逗号.单引号.数字字符串开头有0 ...
- NC235745 拆路
题目链接 题目 题目描述 有 \(n\) 个城镇,城镇之间有 \(m\) 条道路相连,道路可以看成无向边.每一个城镇都有自己的一个繁荣度 \(v_i\) ,一个城镇 \(u\) 受到的影响 \(p\) ...
- Freaktab将于12月底关闭
出过众多优秀固件的电视盒子论坛Freaktab, 将于2021年12月31日关闭 R.I.P
- phpBB3在Nginx反向代理中的X-Forwarded-For IP检查
记录一下phpBB3对反向代理中的IP处理机制 处理几个phpBB3项目迁移, 部分运行场景转移到内网, 需要外网通过nginx/openresty之类的网关反向代理进行访问. 在网关处已经正确配置了 ...
- ex命令
ex命令 ex命令用于在Ex模式下启动vim文本编辑器,ex执行效果如同vi -e,如要从Ex模式回到普通模式,则在vim中输入:vi或:visual指令即可,可以通过运行vi -e来启动ex,也可以 ...
- Oracle正则表达式实战
原文链接:http://oracle-base.com/articles/misc/regular-expressions-support-in-oracle.php Introduction Exa ...
- Spring Boot整合JWT实现接口访问认证
最近项目组需要对外开发相关API接口,需要对外系统进行授权认证.实现流程是先给第三方系统分配appId和appSecret,第三方系统调用我getToken接口获取token,然后将token填入Au ...
- win32 - 控制台聊天
仅适用于同一台电脑的两个进程聊天,对于不同电脑之前的聊天需要依靠tcp/ip协议. 两个进程是通过发送WM_COPYDATA 消息来传输字节的. 代码如下: Server.cpp #include & ...
- Python3中的“联动”现象
技术背景 在python中定义一个列表时,我们一定要注意其中的可变对象的原理.虽然python的语法中没有指针,但是实际上定义一个列表变量时,是把变量名指到了一个可变对象上.如果此时我们定义另外一个变 ...