Educational Codeforces Round 32 B. Buggy Robot(模拟)

曾经终败给现在 2022-06-06 13:25 213阅读 0赞

B. Buggy Robot

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform:

  • U — move from the cell (x, y) to (x, y + 1);
  • D — move from (x, y) to (x, y - 1);
  • L — move from (x, y) to (x - 1, y);
  • R — move from (x, y) to (x + 1, y).

Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!

Input

The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100).

The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R.

Output

Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.

Examples

input

  1. 4
  2. LDUR

output

  1. 4

input

  1. 5
  2. RRRUU

output

  1. 0

input

  1. 6
  2. LLRRRR

output

  1. 4

题解:

这题还是比较水的,直接记录下四个方向执行了几次然后取相反方向的最小值乘2就是答案

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<queue>
  5. #include<stack>
  6. #include<math.h>
  7. #include<vector>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<cmath>
  12. #include<string>
  13. #include<algorithm>
  14. #include<iostream>
  15. using namespace std;
  16. #define lson k*2
  17. #define rson k*2+1
  18. #define M (t[k].l+t[k].r)/2
  19. #define ll long long
  20. int main()
  21. {
  22. int n,i,j;
  23. int x=0,y=0,ans=0;
  24. char s[105];
  25. int a[5];
  26. memset(a,0,sizeof(a));
  27. scanf("%d%s",&n,s);
  28. for(i=0;i<strlen(s);i++)
  29. {
  30. switch(s[i])
  31. {
  32. case 'L':a[0]++;break;
  33. case 'R':a[1]++;break;
  34. case 'U':a[2]++;break;
  35. case 'D':a[3]++;break;
  36. }
  37. }
  38. if(a[0]!=0&&a[1]!=0)
  39. {
  40. ans+=min(a[0],a[1])*2;
  41. }
  42. if(a[2]!=0&&a[3]!=0)
  43. {
  44. ans+=min(a[2],a[3])*2;
  45. }
  46. printf("%d\n",ans);
  47. return 0;
  48. }

发表评论

表情:
评论列表 (有 0 条评论,213人围观)

还没有评论,来说两句吧...

相关阅读