Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)
链接:
https://codeforces.com/contest/1272/problem/B
题意:
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0,0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x,y) right now, he can move to one of the adjacent cells (depending on the current instruction).
If the current instruction is 'L', then the robot can move to the left to (x−1,y);
if the current instruction is 'R', then the robot can move to the right to (x+1,y);
if the current instruction is 'U', then the robot can move to the top to (x,y+1);
if the current instruction is 'D', then the robot can move to the bottom to (x,y−1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0,0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0,0), performs the given instructions, visits no cell other than (0,0) two or more times and ends the path in the cell (0,0). Also cell (0,0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0,0)) and "UUDD" (the cell (0,1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
思路:
从原点出去,再回来,往右边走的布数等同于往左边走的步数,上下同理。
代码:
#include<bits/stdc++.h>
using namespace std;
char s[100010];
int main()
{
int t;
cin >> t;
while(t--)
{
cin >> s;
map<char, int> Mp;
int len = strlen(s);
for (int i = 0;i < len;i++)
Mp[s[i]]++;
if (Mp['U'] == 0 || Mp['D'] == 0)
{
if (Mp['L'] > 0 && Mp['R'] > 0)
cout << 2 << endl << "LR" << endl;
else
puts("0");
}
else if (Mp['L'] == 0 || Mp['R'] == 0)
{
if (Mp['U'] > 0 && Mp['D'] > 0)
cout << 2 << endl << "UD" << endl;
else
puts("0");
}
else
{
int row = min(Mp['L'], Mp['R']);
int col = min(Mp['U'], Mp['D']);
cout << 2*(row+col) << endl;
for (int i = 1;i <= row;i++)
cout << "L";
for (int i = 1;i <= col;i++)
cout << "U";
for (int i = 1;i <= row;i++)
cout << "R";
for (int i = 1;i <= col;i++)
cout << "D";
cout << endl;
}
}
return 0;
}
Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)的更多相关文章
- 【cf比赛记录】Codeforces Round #605 (Div. 3)
比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...
- Codeforces Round #605 (Div. 3)
地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...
- Codeforces Round #605 (Div. 3) 题解
Three Friends Snow Walking Robot Yet Another Broken Keyboard Remove One Element Nearest Opposite Par ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round #180 (Div. 2) A. Snow Footprints 贪心
A. Snow Footprints 题目连接: http://www.codeforces.com/contest/298/problem/A Description There is a stra ...
- Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity
题目链接:http://codeforces.com/contest/1272/problem/E 题意:给定n,给定n个数a[i],对每个数输出d[i]. 对于每个i,可以移动到i+a[i]和i-a ...
- Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)
链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...
- Codeforces Round #605 (Div. 3) D. Remove One Element(DP)
链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...
- Codeforces Round #605 (Div. 3) C. Yet Another Broken Keyboard
链接: https://codeforces.com/contest/1272/problem/C 题意: Recently, Norge found a string s=s1s2-sn consi ...
随机推荐
- PAT(B) 1030 完美数列 - C语言 - 滑动窗口 & 双指针
题目链接:1030 完美数列 (25 point(s)) 给定一个正整数数列,和正整数 \(p\),设这个数列中的最大值是 \(M\),最小值是 \(m\),如果 \(M≤mp\),则称这个数列是完美 ...
- 生意bisynes商业
1.Of, to, pertaining to or utilized for purposes of conducting trade, commerce, governance, advocacy ...
- ArcGIS Engine中C#开发不能引用ESRI.ArcGIS.AxControls问题
问题:ArcGIS Engine中C#开发不能引用ESRI.ArcGIS.AxControls问题 解决方法:将这里的特定版本改成“False”即可.
- Vert.x 异步访问数据库 MySQL
Vert.x提供异步访问数据库的API,数据库操作是一个耗时操作,使用传统的同步模型,容易阻塞线程,导致整体性能下降,因此我们对于数据库操作,需要使用Vert.x提供的异步API. Vert.x提供的 ...
- Python中的 x+=x 与 x = x + x的区别
对于Python中的可变数据类型(列表,字典)来说,+= 和 ..=..+..是不同的 加等是直接在变量的值上面进行操作,会修改了原来变量的值 先等后加会重新分配一个内存空间,不会再原有的变量值上面进 ...
- 【面试突击】-缓存击穿(布隆过滤器 Bloom Filter)
原文地址:https://blog.csdn.net/fouy_yun/article/details/81075432 前面的文章介绍了缓存的分类和使用的场景.通常情况下,缓存是加速系统响应的一种途 ...
- 深入理解es6(上)
一.let和const 1.let与var的区别 不存在变量提升 块级作用域 不允许重复声明 2.const常量 const与let一样,唯一区别在于声明的常量不能被修改 二.解构赋值 es6按照一定 ...
- 【转载】TX - row lock contention 的一些场景
TX - row lock contention 的一些场景 原创 2016-07-11 易欣 云和恩墨 易欣(Eson) 云和恩墨技术专家 本文整理来自7月7日周四晚云和恩墨大讲堂嘉宾易欣分享的主题 ...
- 在地址栏里输入一个 URL后,按下 Enter 到这个页面呈现出来,中间会发生什么?
这是一个面试高频的问题 在输入 URL 后,首先需要找到这个 URL 域名的服务器 IP,为了寻找这个 IP,浏览器首先会寻找缓存,查看缓存中是否有记录,缓存的查找记录为:浏览器缓存 ->系统缓 ...
- sklearn.feature_extraction.text 的TfidfVectorizer函数
TfidfVectorizer函数主要用于,将文档(句子)等通过 tf-idf值来进行表示,也就是用一个tf-idf值的矩阵来表示文档(句子也可). from sklearn.feature_extr ...