先进行压缩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. 详解 & 0xff 的意义及作用

    首先我们要都知道, &表示按位与,只有两个位同时为1,才能得到1, 0x代表16进制数,0xff表示的数二进制1111 1111 占一个字节.和其进行&操作的数,最低8位,不会发生变化 ...

  2. Linux--uniq 命令(检查和处理重复行的数据)

    文本中的重复行,基本上不是我们所要的,所以就要去除掉.linux下有其他命令可以去除重复行,但是我觉得uniq还是比较方便的一个. 使用uniq的时候要注意以下二点1.对文本操作时,它一般会和sort ...

  3. 【译】我为 .NET 开发人员准备的 2023 年 Visual Studio 10 大新功能

    原文 | James Montemagno 翻译 | 郑子铭 Visual Studio 2022 在 2023 年发布了许多令人难以置信的功能,为 .NET 开发人员提供了大量新工具来提高他们的工作 ...

  4. 如何用低代码实现批量导出PDF?

    前言 事情是这样的,熟悉我们的朋友都知道,我司有一个为广大开发者朋友们提供学习帮助的地方,叫做新手训练营,具体的内容就是会针对初次接触葡萄城产品和技术的用户,通过 2-3 天的集中学习,采用直播授课的 ...

  5. UVA11573 Ocean Currents

    题目链接 题目 见链接. 题解 知识点:BFS. 这道题显然用BFS,但发现洋流方向会破坏时间的有序性,但注意到洋流时间花费是 \(0\) ,因此只需要用双端队列即可,洋流方向扩展直接放队头,其他方向 ...

  6. NC13230 合并回文子串

    题目链接 题目 题目描述 输入两个字符串A和B,合并成一个串C,属于A和B的字符在C中顺序保持不变.如"abc"和"xyz"可以被组合成"axbycz ...

  7. Javascript操作对象数组实现增删改查

    1.介绍 最近帮朋友弄一个简单的针对json数组的增删改成页面,正好涉及到了js去操作对象数组实现增删改查功能.我估计很多朋友应该也会遇到这类操作,所以记录一下以便分享. 2.数据准备 这里我就以学生 ...

  8. Vmware中Linux通过NAT设置静态IP实现上网

    1.设置虚拟机上网方式为NAT 2.修改centos网络配置文件,我的是centos7.4,主要网关不能和主机设置的一致 [root@dylan-centos ~]# vi /etc/sysconfi ...

  9. Innodb存储引擎的文件

    目录 概述 参数文件 日志文件 错误日志 慢查询日志 查询日志 二进制日志 binary log 二进制日志的配置 二进制日志的作用 二进制日志的保存 socket 套接字文件 pid文件 MySQL ...

  10. python 创建动态类

    一般情况下多数是预先定义类 而少数特殊情况就需要去动态创建类了,直接贴代码. class BaseModel(Model): class Meta: database = _tb class_new ...