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

来源:互联网 发布:手机房屋平面设计软件 编辑:程序博客网 时间:2024/06/05 18:34

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 UDL 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
4LDUR
output
4
input
5RRRUU
output
0
input
6LLRRRR
output
4


题解:

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

代码:

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>using namespace std;#define lson k*2#define rson k*2+1#define M (t[k].l+t[k].r)/2#define ll long longint main(){    int n,i,j;    int x=0,y=0,ans=0;    char s[105];    int a[5];    memset(a,0,sizeof(a));    scanf("%d%s",&n,s);    for(i=0;i<strlen(s);i++)    {        switch(s[i])        {            case 'L':a[0]++;break;            case 'R':a[1]++;break;            case 'U':a[2]++;break;            case 'D':a[3]++;break;        }    }    if(a[0]!=0&&a[1]!=0)    {        ans+=min(a[0],a[1])*2;    }    if(a[2]!=0&&a[3]!=0)    {        ans+=min(a[2],a[3])*2;    }    printf("%d\n",ans);return 0;}