2019/12/12 CF-1272-B Snow Walking Robot

Codeforces Round #605 (Div. 3)

题目链接:http://codeforces.com/contest/1272/problem/B

题意大意:

       尽可能少的删除无效或多余指令,并重新排列指令使机器人从原点出发,最后回到原点,中间不能经过同一个点两次(原点也仅能出发和结束经过)。

测试样例:

input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL

output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0

Note

There are only two possible answers in the first test case: "LR" and "RL".

The picture corresponding to the second test case:

 

Note that the direction of traverse does not matter

Another correct answer to the third test case: "URDDLLLUURDR".

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
char s[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        int len=strlen(s);
        int cnt[4];
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<len;i++)
        {
            if(s[i]=='U')
                cnt[0]++;
            if(s[i]=='D')
                cnt[1]++;
            if(s[i]=='L')
                cnt[2]++;
            if(s[i]=='R')
                cnt[3]++;
        }
        int mini1=0,mini2=0;
        //找到一组指令中较少个数的指令,以其个数为标准
        mini1=min(cnt[0],cnt[1]);
        mini2=min(cnt[2],cnt[3]);
        //若一组指中令有一个为0,则那一组指令都为0,且另一组两个只能走一步
        if(mini1==0&&mini2!=0)
            mini2=1;
        if(mini2==0&&mini1!=0)
            mini1=1;
        int sum=2*(mini1+mini2);
        printf("%d\n",sum);
        //按照自己设定的规则走,不重复且回到原点
        for(int i=0;i<mini1;i++)
            printf("U");
        for(int i=0;i<mini2;i++)
            printf("L");
        for(int i=0;i<mini1;i++)
            printf("D");
        for(int i=0;i<mini2;i++)
            printf("R");
        printf("\n");
    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值