先进行压缩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. DbgridEh 1900-01-01 00:00:00 问题解决

    --------------------------------------------------

  2. Python-wxauto微信自动发送消息或文件

    1.安装wxauto和pyautogui库,pip安装即可. pip install wxauto pip install pyautogui 2.登录电脑微信客户端 这里有两个注意点:(1)不能将客 ...

  3. NC14699 队伍配置

    题目链接 题目 题目描述 萌学姐在玩大型手游<futa go>,他现在准备进入作战环节,所以他准备安排自己的队伍. 队伍配置里,可供玩家选择的作战人物被称作"从者",玩 ...

  4. 面试官:谈一谈你对 redis 分布式锁的理解

    ​为什么需要分布式锁 在 jdk 中为我们提供了多种加锁的方式: (1)synchronized 关键字 (2)volatile + CAS 实现的乐观锁 (3)ReadWriteLock 读写锁 ( ...

  5. Centos8 安装 Redis6.0.16

    下载,解压,编译,安装 安装至 /opt/redis/redis-6.0.16 目录 tar xvf redis-6.0.16.tar.gz gcc --version cd redis-6.0.16 ...

  6. 【Unity3D】异步Socket通讯

    1 前言 ​ 同步 Socket 通讯 中的 Accept.Connect.Receive 等方法会阻塞当前线程,当前线程必须等待这些方法执行完,才会继续往下执行,用户需要另开线程执行这些耗时方法,否 ...

  7. 【Lua】Lua基础语法

    1 Lua 简介 ​ Lua 是一个小巧的脚本语言,用标准C语言编写而成,由巴西里约热内卢天主教大学的 Roberto Ierusalimschy.Waldemar Celes 和 Luiz Henr ...

  8. ORA-39087: Directory Name Is Invalid

    说明 有时我们在Oracle数据库服务器执行expdp/impdp过程中会碰到这个错误:ORA-39087: Directory Name Is Invalid,意思是我们指定的directory参数 ...

  9. BUG管理系统(Mantis)迁移实战

    Mantis迁移实战 名词解释 Mantis:  开源的BUG管理平台Mantis,也做MantisBT.           同档次产品有EasyBUG,QC,BugFree,Bugzila. Xa ...

  10. dp题单——区间dp

    一.基本概念 1.链式区间dp for(int len = 2; len <= n; len++){ //枚举区间长度 for(int i = 1; i + len - 1 <= n; i ...