CF1272 B DIV3 ---代码对比
Posted baizijianyidi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1272 B DIV3 ---代码对比相关的知识,希望对你有一定的参考价值。
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
- ;
- if the current instruction is ‘D‘, then the robot can move to the bottom to (x,y
- .
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.
The first line of the input contains one integer q
) — 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 105characters ‘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).
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
, you are allowed to print an empty line (but you can don‘t print it).
6 LRU DURLDRUDRULRDURDDL LRUDDLRUDRUL LLLLRRRR URDUR LLL
2 LR 14 RUURDDDDLLLUUR 12 ULDDDRRRUULL 2 LR 2 UD 0
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
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 LR "); 23 return ; 24 } 25 if(nums[2]==0||nums[3]==0) 26 if(nums[1]>0&&nums[0]>0){ 27 printf("2 UD "); 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 "); 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]=‘