CF1272 B DIV3 ---代码对比

这次DIV3有点可惜啊,题解是我的与学长的代码对比

学长的原博客https://www.cnblogs.com/xyq0220/p/12036109.html

B.Snow Walking Robot

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line of the input contains one integer q

(1≤q≤2⋅104

) — the number of test cases.

The next q

lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 105

characters ‘L‘, ‘R‘, ‘U‘ and ‘D‘ — the initial sequence of instructions.

It is guaranteed that the sum of |s|

(where |s| is the length of s) does not exceed 105 over all test cases (∑|s|≤105

).

Output

For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t

the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0

, you are allowed to print an empty line (but you can don‘t print it).

Example

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

先上学长的代码:

因为是无限大的网格,所以最简单的构造方法是走一个矩形框

注意要特判向任意方向走一步再走回起点的情况,样例中有。

 1 #include<bits/stdc++.h>
 2 #define fi first
 3 #define se second
 4 #define lson l,mid,p<<1
 5 #define rson mid+1,r,p<<1|1
 6 #define pb push_back
 7 #define ll long long
 8 using namespace std;
 9 const int inf=1e9;
10 const int mod=1e9+7;
11 const int maxn=1e5+10;
12 int q;
13 char s[maxn];
14 int main(){
15     ios::sync_with_stdio(false);
16     //freopen("in","r",stdin);
17     cin>>q;
18     while(q--){
19         cin>>s+1;
20         int n=strlen(s+1);
21         int L=0,R=0,U=0,D=0;
22         for(int i=1;i<=n;i++){
23             if(s[i]==‘L‘) ++L;
24             else if(s[i]==‘R‘) ++R;
25             else if(s[i]==‘U‘) ++U;
26             else ++D;
27         }
28         L=R=min(L,R);
29         U=D=min(U,D);
30         if(L&&U==0){
31             cout<<2<<endl;
32             cout<<"RL"<<endl;
33         }else if(U&&L==0){
34             cout<<2<<endl;
35             cout<<"DU"<<endl;
36         }else if(U==0||L==0){
37             cout<<0<<endl;
38         }else{
39             cout<<L*2+U*2<<endl;
40             for(int i=1;i<=L;i++){
41                 cout<<‘R‘;
42             }
43             for(int i=1;i<=D;i++){
44                 cout<<‘D‘;
45             }
46             for(int i=1;i<=L;i++){
47                 cout<<‘L‘;
48             }
49             for(int i=1;i<=D;i++){
50                 cout<<‘U‘;
51             }
52             cout<<endl;
53         }
54     }
55     return 0;
56 }

再来我的

  1     #include <iostream>
  2     #include <cstring>
  3     using namespace std;
  4
  5     void fun(void){
  6         char pose[100005],put[100005];
  7         int nums[4]={0},lth;
  8         scanf("%s",&pose);
  9         lth = strlen(pose);
 10         for(int i = 0;i<lth;i++){
 11             if(pose[i]==‘U‘)
 12                 nums[0] += 1;
 13             else if(pose[i]==‘D‘)
 14                     nums[1]+=1;
 15                 else if(pose[i]==‘L‘)
 16                         nums[2]+=1;
 17                     else if(pose[i]==‘R‘)
 18                             nums[3]+=1;
 19         }
 20         if(nums[0]==0||nums[1]==0)
 21             if(nums[2]>0&&nums[3]>0){
 22                 printf("2\nLR\n");
 23                 return ;
 24             }
 25         if(nums[2]==0||nums[3]==0)
 26             if(nums[1]>0&&nums[0]>0){
 27                 printf("2\nUD\n");
 28                 return ;
 29             }
 30         if((nums[0]==nums[1]&&nums[0]==0&&nums[3]!=1&&nums[2]!=1)||(nums[2]==nums[3]&&nums[2]==0&&nums[0]!=1&&nums[1]!=1)){
 31             printf("0\n");
 32             return;
 33             }
 34
 35         int hmin,lmin,idx1,idx2,count=0;
 36         if(nums[0]>=nums[1]){
 37             idx1 = 1;
 38             lmin = nums[1];
 39         }
 40         else{
 41             idx1 = 0;
 42             lmin = nums[0];
 43         }
 44         if(nums[2]>=nums[3]){
 45             idx2 = 3;
 46             hmin = nums[3];
 47         }
 48         else{
 49             idx2 = 2;
 50             hmin = nums[2];
 51         }
 52         int j =0;
 53         for(int i = 0;i<lmin;i++){
 54             if(idx1){
 55                 put[j]=‘D‘;
 56                 j++;
 57                 count+=1;
 58             }
 59
 60             else{
 61                 put[j]=‘U‘;
 62                 j++;
 63                 count+=1;
 64             }
 65
 66         }
 67         for(int i = 0;i<hmin;i++){
 68             if(idx2==3){
 69                 put[j]=‘L‘;
 70                 j++;
 71                 count+=1;
 72             }
 73
 74             else{
 75                 put[j]=‘R‘;
 76                 j++;
 77                 count += 1;
 78             }
 79
 80         }
 81         for(int i = 0;i<lmin;i++){
 82             if(idx1){
 83                 put[j]=‘U‘;
 84                 j++;
 85                 count+=1;
 86             }
 87
 88             else{
 89                 put[j]=‘D‘;
 90                 j++;
 91                 count +=1;
 92             }
 93
 94         }
 95         for(int i = 0;i<hmin;i++){
 96             if(idx2==3){
 97                 put[j]=‘R‘;
 98                 j++;
 99                 count+=1;
100             }
101
102             else{
103                 put[j]=‘L‘;
104                 j++;
105                 count+=1;
106             }
107
108         }
109         put[j]=‘\0‘;
110         if(strlen(put)>lth){
111             count -= 1;
112             put[j-1]=‘\0‘;
113         }
114         printf("%d\n%s\n",count,put);
115         return ;
116     }
117
118     int main(){
119         int n;
120         cin>>n;
121         for(int i = 0;i<n;i++){
122             fun();
123         }
124         return 0;
125     }

先从时间吧,时间都是N^2,但学长的循环数量明显少于我的循环数量

内存:我开了两个100005的字符串而学长只有一个

这题不涉及算法,就是模拟与思维

很明显,我们的思路大同小异,但是代码简洁度就是天上与地下。

  1.在统计四个字母个数时学长用的是整型变量,而我用的是数组,现在想想确实使用变量会比较好,打的东西少,操作方便,看起来舒服。再来就是循环嵌套,个人习惯性问题,可能我的这种阶梯式写法对于一层执行语句不简洁,但是多条执行语句的话,方便分清结构。

  2.特判输出,学长直接循环嵌套,而我则是每种情况列举、输出、然后return,感觉学长的嵌套比较香,因为return可能会造成我现在还没有遇到的BUG,比如我退出多层循环喜欢使用GOTO,但是北理的老师不是这么允许他们的学生使用goto因为goto在面对大型程序时会产生一些bug。(这个也是道听途说,但真实性还是比较高的,希望以后有机会写大型程序的时候试试会有什么BUG)

  3.答案输出,其实因为思路大同小异,所以都差不多,但是学长的代码是真的简单,加油吧。

原文地址:https://www.cnblogs.com/baizijianyidi/p/12048347.html

时间: 2024-08-30 18:18:51

CF1272 B DIV3 ---代码对比的相关文章

代码对比工具Sublime——Sublimerge

怎样用Sublime Text对比查找两个文档的不同 由于Sublime Text这个编辑器,除了可长久试用外,甚至还有许多插件可安装,让原来只有简易文字的编辑器,功能变得更加强大. 具体步骤参考链接:https://jingyan.baidu.com/article/948f5924eee15dd80ff5f9ab.html 本文简要记录,重点介绍出现的问题和解决办法. 1. 安装Sublime 官网下载安装包:https://www.sublimetext.com/ 如Windows安装包:

Java程序员最常用的6个代码对比工具,架构师一定收藏

在Java程序开发的过程中,程序员会经常对源代码以及库文件进行代码对比,那么今天在这篇文章里我们给大家介绍六款程序员常用的代码比较工具,希望对大家会有帮助. WinMerge WinMerge是一款运行于Windows系统下的文件比较和合并工具,使用它可以非常方便地比较多个文档内容,适合程序员或者经常需要撰写文稿的朋友使用.WinMerge会将两个文件内容做对比,并在相异之处以高亮度的方式显示,让使用者可以很快的查知:可以直接让左方的文件内容直接覆盖至右方,或者反过来也可以覆盖.Diffuse

圣思源Java视频36节练习源码分享(自己的190+行代码对比老师的39行代码)

题目: * 随机生成50个数字(整数),每个数字范围是[10,50],统计每个数字出现的次数 * 以及出现次数最多的数字与它的个数,最后将每个数字及其出现次数打印出来, * 如果某个数字出现次数为0,则不要打印它.打印时按照数字的升序排列. 要求: * 使用数组的知识完成此功能,不能使用JDK的API函数. 分析: * 需要写一个随机数生成函数 * 需要写一个冒泡排序函数 * 需要写一个二分查找获取数组中某元素个数的函数 代码如下: 1 package Array32; 2 3 /** 4 *

利用 IntelliJ IDEA 进行代码对比的方法

Sometimes,我们会有这样的需求,即:想对比出两个不同版本代码的区别.如何实现? 第 1 种:如果我们是从 SVN 检出的项目,并且想比较本地代码与从 SVN 检出时的代码相比都有那些区别,可以按如下步骤操作, 如上图所示,在代码编辑区,右键唤出功能菜单,然后选择Subversion,进而会展示出更多的可选项,例如: Compare with the Same Repository Version,与 SVN 仓库相同版本做对比:Compare with Latest Repository

JAVA Httpclient3.x与Httpclient4.x代码对比(post方法)

Httpclient4.x post方法代码: 1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.io.UnsupportedEncodingException; 6 import java.util.ArrayList; 7 import java.util.L

中英文代码对比系列之Java一例

原文: https://zhuanlan.zhihu.com/p/30905033. 作者为本人. 这个系列将对同一段代码进行中文命名和英文命名两个版本的比较. 目的包括, 演示中文命名, 发现命名时可能遇到的问题, 探讨代码风格(中文命名的'套路')等. 示例中的命名风格仅基于个人非常有限的实践, 希望抛砖引玉. 不在讨论范围内的是: 中英文代码的可读性孰高孰低. 个人相信用英文和中文都能写出可读性很好的代码. 区别仅在于母语不同的开发者对哪个版本更敏感, 读写维护起来更省工. 原代码本身的优

JDK6和JDK7中String类下的substring方法的代码对比(仅贴代码,未详述)

返回主页 回到顶端 jdk1.6版本String.java文件中关于substring的代码及描述 1 /** 2 * Returns a new string that is a substring of this string. The 3 * substring begins with the character at the specified index and 4 * extends to the end of this string. <p> 5 * Examples: 6 *

linux platform device/driver(三)--Platform Device和Platform_driver注册过程之代码对比

转自:http://blog.csdn.net/thl789/article/details/6723350 Linux 2.6的设备驱动模型中,所有的device都是通过Bus相连.device_register() / driver_register()执行时通过枚举BUS上的Driver/Device来实现绑定,本文详解这一过程.这是整个LINUX设备驱动的基础,PLATFORM设备,I2C上的设备等诸设备的注册最终也是调用本文讲述的注册函数来实现的. Linux Device的注册最终都

eclipse使用图型svn代码对比 上传功能