XiaoHuang's Space
XiaoHuang's Space
XiaoHuang
May the force be with you.
『题解』Codeforces888B Buggy Robot
Posted: Dec 11, 2019
Last Modified: Dec 13, 2019
This article was last modified days ago. The content of this post may be outdated!

Portal

Portal1: Codeforces

Portal2: Luogu

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:

  • $\mathrm{U}$ — move from the cell $(x, y)$ to $(x, y + 1)$;

  • $\mathrm{D}$ — move from $(x, y)$ to $(x, y - 1)$;

  • $\mathrm{L}$ — move from $(x, y)$ to $(x - 1, y)$;

  • $\mathrm{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 \leq n \leq 100)$.

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

Output

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

Sample Input1

4
LDUR

Sample Output1

4

Sample Input2

5
RRRUU

Sample Output2

0

Sample Input3

6
LLRRRR

Sample Output3

4

Description in Chinese

给你一个字符串,让你求$2 \times (\min(\texttt{‘L’的个数, ‘R’的个数}) + \min(\texttt{‘U’的个数,’D’的个数}))$。

Solution

给你一个字符串,先计算出$\mathrm{L}$,$\mathrm{R}$,$\mathrm{U}$,$\mathrm{D}$的个数,最后按题目给出的表达式输出即可。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

int n;
string st;
int main() {
    scanf("%d", &n);
    cin >> st;
    int L = 0, R = 0, U = 0, D = 0;
    for (int i = 0; i < st.length(); i++)
        if (st[i] == 'L') L++; else
        if (st[i] == 'R') R++; else
        if (st[i] == 'U') U++; else
        if (st[i] == 'D') D++;//统计数量
    printf("%d\n", (min(L, R) + min(U, D)) << 1);
    return 0;
}
Article License: CC BY-NC-ND 4.0
Article Author: XiaoHuang
  1. 1. Portal
  2. 2. Description
  3. 3. Input
  4. 4. Output
  5. 5. Sample Input1
  6. 6. Sample Output1
  7. 7. Sample Input2
  8. 8. Sample Output2
  9. 9. Sample Input3
  10. 10. Sample Output3
  11. 11. Description in Chinese
  12. 12. Solution
  13. 13. Code
Newer Post
『题解』Codeforces888D Almost Identity Permutations
Older Post
『题解』Codeforces735D Taxes
Buy me a beer?
-->
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×