链接:

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(构造)的更多相关文章

  1. 【cf比赛记录】Codeforces Round #605 (Div. 3)

    比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...

  2. Codeforces Round #605 (Div. 3)

    地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...

  3. Codeforces Round #605 (Div. 3) 题解

    Three Friends Snow Walking Robot Yet Another Broken Keyboard Remove One Element Nearest Opposite Par ...

  4. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  5. Codeforces Round #180 (Div. 2) A. Snow Footprints 贪心

    A. Snow Footprints 题目连接: http://www.codeforces.com/contest/298/problem/A Description There is a stra ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. shoshana-摄影文集

    20190331 冬宫之油画 20190427 [遇见•梦露]画展 20190428 [三极探索之旅]公益摄影 20190504  赏美-第[000]期 20190505 赏美-第[001]期 201 ...

  2. 玩机之Honor_V10

    作为一个热爱手机的Geek,自然是经历了很多的刷机和改装手机的经验,当然翻车的经验也是有的.一般来说的折腾手机都是在遇到某一版本使用以及各方面都比较稳定的时候才会选择让手机停留在哪一版本.下面我就来分 ...

  3. Python与MogoDB交互

    睡了大半天,终于有时间整理下拖欠的MongoDB的封装啦. 首先我们先进行下数据库的连接: conn = MongoClient('localhost',27017) # 建立连接 result = ...

  4. Java 阿拉伯数字转换为中文大写数字

    Java 阿拉伯数字转换为中文大写数字 /** * <html> * <body> * <P> Copyright 1994 JsonInternational&l ...

  5. 1、C#多线程基础理论

    系统为应用程序分配所需的内存以及其他资源,内存和资源的物理分离叫做进程.   进程是以线程为单位竞争CPU,那么什么是线程呢? 线程可看成一个可执行的指令单元,他使用进程中的数据,包含若干条指令,进程 ...

  6. TCP,UDP 通讯的helper类

    使用Tcp通讯,首先要启动tcp服务端监听客户端,客户端发送消息,服务端收到消息 1.服务端代码如下 public class TcpServerTest { public async Task Be ...

  7. java之hibernate之hibernate查询

    这篇主要简单间接 hibernate查询 1.数据库操作中最重要的是查询,Hibernate提供了多种查询方式来帮助程序员快速实现查询功能. 有hql,本地sql查询,Criteria查询,examp ...

  8. 关键字ref、out

    通常,变量作为参数进行传递时,不论在方法内进行了什么操作,其原始初始化的值都不会被影响: 例如: public void TestFun1() { ; TestFun2(arg); Console.W ...

  9. [HNOI2012]矿场搭建 (点双连通)

    题目 [HNOI2012]矿场搭建 解析 这个题做的我十分自闭.. 没看出这个是个点双,然后一晚上+半上午.. 一看肯定和割点有关,我们找到所有的点双,会发现有这么几种情况 连通块中一个割点也没有,这 ...

  10. js节流与防抖函数封装

    js节流与防抖函数封装 常见应用场景: window的 resize 和 scroll 事件: 文字输入时的 keyup 事件: 元素拖拽.移动时的 mousemove 事件: 防抖 定义:多次触发事 ...