先进行压缩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的更多相关文章

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

  2. [atcoder contest 010] F - Tree Game

    [atcoder contest 010] F - Tree Game Time limit : 2sec / Memory limit : 256MB Score : 1600 points Pro ...

  3. LeetCode 979. Distribute Coins in Binary Tree

    原题链接在这里:https://leetcode.com/problems/distribute-coins-in-binary-tree/ 题目: Given the root of a binar ...

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

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

  6. 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  7. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  8. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

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

  10. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

随机推荐

  1. ElasticSearch7.3学习(七)----Mapping映射入门

    1.mapping映射 概念:自动或手动为index中的_doc建立的一种数据结构和相关配置,简称为mapping映射.插入几条数据,让es自动为我们建立一个索引 PUT /website/_doc/ ...

  2. SpringBoot不再需要@Autowired来注入属性

    实操部分 需要lombok依赖 在对应需要注入属性的类上添加注解 @RequiredArgsConstructor 所有需要注入的属性改为final修饰 为什么 lombok的@RequiredArg ...

  3. 2.列表--《Python编程:从入门到实践》

    2.1 列表   列表由一系列按特定顺序排列的元素组成.在Python中,用方括号([])来表示列表,并用逗号来分隔其中的元素. bicycles = ['trek', 'cannondale', ' ...

  4. 扩展说明: 指令微调 Llama 2

    这篇博客是一篇来自 Meta AI,关于指令微调 Llama 2 的扩展说明.旨在聚焦构建指令数据集,有了它,我们则可以使用自己的指令来微调 Llama 2 基础模型. 目标是构建一个能够基于输入内容 ...

  5. SP21690 POWERUP - Power the Power Up 题解

    题目传送门 前置知识 扩展欧拉定理 解法 直接对 \(a\) 和 \(b^c\) 分讨,跑一遍扩展欧拉定理就行了. 另外由于本题的特殊规定 \(0^0=1\),故需要在当 \(a=0\) 时,对 \( ...

  6. k8s-权限管理

    目录 1. 身份认证 node节点操作 创建普通用户并授权 1. 生成私钥 2. 生成zhangsan用户证书请求文件 3. 为zhangsan用户颁发证书 4. 创建命名空间及pod 5. 创建角色 ...

  7. 2022年3月核心库MySQL优化总结

    2021年9月底到新公司,公司核心数据库,以前无专业DBA维护,问题多,隐患大,到2022年2月持续后,优化至今: 优化大项: 1,从1主3从,扩容到1主5从,将部分读放都另外新加从库   2,最大表 ...

  8. Vuepress + GitHub Actions实现文档博客自动部署

    说明 接着我上一篇文章,已经使用vuepress+github pages搭建好了自己的文档博客,并且可以在本地打包上传后就可以在pages上查看更新内容.但是有1个比较明显的缺点:打包速度并不快!! ...

  9. Innodb存储引擎之锁

    目录 一.概述 二.lock 与 latch 三.Innodb存储引擎中的锁 锁 一致性非锁定读 一致性锁定读 自增长与锁 外键与锁 四.锁的算法 锁的算法 Phantom Problem 幻读问题 ...

  10. QT - Day 2

    QMainWindow 菜单栏  最多有一个 QMenuBar *bar = MenuBar(); setMenuBar(bar); QMenu *fileMenu = bar->addMenu ...