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 ...
随机推荐
- [转帖]UML类图新手入门级介绍
UML类图新手入门级介绍 2010-11-12 19:45:00 monkey_d_meng 阅读数 27230 收藏 文章标签: umlinterfaceclass编程扩展更多 分类专栏: 软件工 ...
- 最近公共祖先 LCA 递归非递归
给定一棵二叉树,找到两个节点的最近公共父节点(LCA).最近公共祖先是两个节点的公共的祖先节点且具有最大深度.假设给出的两个节点都在树中存在. dfs递归写法 查找两个node的最近公共祖先,分三种情 ...
- redis源码分析(三)--rdb持久化
Redis rdb持久化 Redis支持两种持久化方式:rdb与aof.rdb将一个节点上的内存数据序列化后存储到磁盘中,序列化的数据以尽可能节约空间的方式存储,并非完全的ascii表示.它的优点在于 ...
- C语言函数返回指针方法
1.将函数内部定义的变量用static修饰 由于static修饰的变量,分配在静态内存区(类似于全局变量区),函数返回时,并不会释放内存,因此可以将要返回的变量加static修饰. int *test ...
- Fiddler抓包8-打断点(bpu)(转)
转自:https://www.cnblogs.com/yoyoketang/p/6778006.html
- elasticsearch内存不断增长问题
经过一段时间运行,es的索引已经达到数十G以上.es采用mmap的方式将索引文件映射到内存中,随着检索的次数增加,越来越多的数据被操作系统读入到内存中.这部分内存位于系统中,但是又不归es管理,也就是 ...
- 通过重新上传修改后的docker镜像来在kubeapps上实现k8s上部署的nginx版本更新,回退等
docker操作:制作自定义镜像 # docker下载官方nginx镜像 docker pull nginx # 基于该镜像运行一个容器 docker run -it -d --name nginx_ ...
- 【转载】 C#中decimal.TryParse方法和decimal.Parse方法的异同之处
在C#编程过程中,decimal.TryParse方法和decimal.Parse方法都可以将字符串string转换为decimal类型,但两者还是有区别,最重要的区别在于decimal.TryPar ...
- 仿百度图片首页--HTML+CSS练手项目1【Table】
[本文为原创,转载请注明出处] 技术[CSS+HTML] 布局[Table] 图片准备[百度图标.10张不同类型图] --------------------------------------- ...
- ORACLE表、索引和分区详解
ORACLE表.索引和分区 一.数据库表 每种类型的表都有不同的特性,分别应用与不同的领域 堆组织表 聚簇表(共三种) 索引组织表 嵌套表 临时表 外部表和对象表 1.行迁移 建表过程中可以指定以下两 ...