先进行压缩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. 基于Doris构建亿级数据实时数据分析系统

    转载至我的博客 https://www.infrastack.cn ,公众号:架构成长指南 背景 随着公司业务快速发展,对业务数据进行增长分析的需求越来越迫切,与此同时我们的业务数据量也在快速激增.每 ...

  2. Host跨网段的ARP request

    写得很清楚的文章:ARP协议在同网段及跨网段下的工作原理 对应的官方ARP文档:RFC 826 对文档的解读:详解RFC 826文档 ARP跨网段进行request包发送,需要先检测自身子网是否与对方 ...

  3. NC26257 小雨坐地铁

    题目链接 题目 题目描述 小雨所在的城市一共有 \(m\) 条地铁线,分别标号为 1 号线,2 号线,--,m 号线.整个城市一共有 \(n\) 个车站,编号为 \(1 \sim n\) .其中坐 i ...

  4. NC14685 加边的无向图

    题目链接 题目 题目描述 给你一个 n 个点,m 条边的无向图,求至少要在这个的基础上加多少条无向边使得任意两个点可达~ 输入描述 第一行两个正整数 n 和 m . 接下来的m行中,每行两个正整数 i ...

  5. NC15136 迷宫

    题目链接 题目 题目描述 这是一个关于二维迷宫的题目.我们要从迷宫的起点 'S' 走到终点 'E',每一步我们只能选择上下左右四个方向中的一个前进一格. 'W' 代表墙壁,是不能进入的位置,除了墙壁以 ...

  6. NC207751 牛牛的旅游纪念品

    题目链接 题目 题目描述 牛牛在牛市的旅游纪念商店里面挑花了眼,于是简单粗暴的牛牛决定--买最受欢迎的就好了. 但是牛牛的背包有限,他只能在商店的n个物品里面带m个回去,不然就装不下了. 并且牛牛希望 ...

  7. 【分布式】load balance 02-consistent hash algorithm 一致性哈希算法原理详解

    负载均衡系列专题 01-负载均衡基础知识 02-一致性 hash 原理 03-一致性哈希算法 java 实现 04-负载均衡算法 java 实现 概念 一致哈希是一种特殊的哈希算法. 在使用一致哈希算 ...

  8. 使用JS访问本地数据库

    1 前言 有时候,数据业务比较大,比如查询百万级的数据,如果使用JSP查询数据库,JSP的返回结果一般放在域名后面返回给客户端,而返回结果的长度是有限制的,数据过长可能会丢失部分数据:另一方面数据量大 ...

  9. 【OpenGL ES】EGL+FBO离屏渲染

    1 前言 ​ FBO离屏渲染 中使用 GLSurfaceView 来驱动 Renderer 渲染图片,为了隐藏 GLSurfaceView,将其设置为透明的,并且宽高都设置为1.本文将使用 EGL 代 ...

  10. Java设计模式-备忘录模式Memento

    介绍 备忘录模式(Memento Pattern)在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 可以这里理解备忘录模式:现实生 ...