CodeForces 888B Buggy Robot

来源:互联网 发布:淘宝管理费 编辑:程序博客网 时间:2024/05/18 04:59

Description:

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.

Example
Input
4LDUR
Output
4
Input
5RRRUU
Output
0
Input
6LLRRRR
Output
4

题目大意:

个机器人有如下的操作, 向上(U), 向下(D), 向左(L)和向右(R)。起初机器人在远点, 现在给机器人一条指令, 问机器人所能走的最大步数回到原点的值是多少。

解题思路:

我们可以贪心的走, 一对LR或一对UD都满足条件, 所以我们只要统计四种操作的个数, 答案就能计算出是2 * min(l, r)+ 2 * min(u, d)。

代码:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>

using namespace std;

/*
 *ios::sync_with_stdio(false);
 */

typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 1e6;

int n;
char arr[Max];

int main() {
    //freopen("input.txt", "r", stdin);
    scanf("%d", &n);
    scanf("%s", arr);
    int l = 0, r = 0, u = 0, d = 0;
    for (int i = 0; i < n; ++i) {
        if (arr[i] == 'L') l++;
        else if (arr[i] == 'R') r++;
        else if (arr[i] == 'U') u++;
        else if (arr[i] == 'D') d++;
    }
    int ans = 2 * min(l , r) + 2 * min(u, d);
    printf("%d\n", ans);
    return 0;
}



原创粉丝点击