CF712B.Memory and Trident

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:

  • An 'L' indicates he should move one unit left.
  • An 'R' indicates he should move one unit right.
  • A 'U' indicates he should move one unit up.
  • A 'D' indicates he should move one unit down.

But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.

Memory 正在二维平面上从原点出发行走。他获得了一个字符串 $ s $,其中包含其移动方向:

  • 字符 'L' 表示向左移动一个单位;
  • 字符 'R' 表示向右移动一个单位;
  • 字符 'U' 表示向上移动一个单位;
  • 字符 'D' 表示向下移动一个单位。

但现在 Memory 希望最终回到原点。为此,他拥有一件特殊的三叉戟。该三叉戟可将字符串 $ s $ 中任意位置的字符替换为 'L'、'R'、'U' 或 'D' 中的任意一个。然而,由于他不想过度损耗三叉戟,他希望进行尽可能少的修改。请告诉 Memory:最少需要多少次修改,才能使字符串对应的行走路径最终回到原点?若不存在这样的字符串,则说明无解。

输入格式

The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.

第一行且唯一一行包含字符串 ss(1 ≤ ∣s∣ ≤ 100 0001 ≤ |s| ≤ 100\,000)—— Memory 所收到的指令。

输出格式

If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.

如果存在满足条件的字符串,则输出一个整数——所需的最少编辑次数;若无法通过修改序列使 Memory 回到原点,则输出 −1-1。

输入输出样例

  • 输入#1

    RRU

    输出#1

    -1
  • 输入#2

    UDUR

    输出#2

    1
  • 输入#3

    RUUR

    输出#3

    2

说明/提示

In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.

In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.

在第一个样例测试中,Memory 被指示先向右走,再向右走,最后向上走。显然,无法通过编辑这些指令来构成一条合法的行走路径。

在第二个样例测试中,Memory 被指示先向上走,再向下走,然后向上走,最后向右走。一种可能的解法是将字符串 $ s $ 修改为 "LDUR"。该字符串仅需 1 次编辑,这是可能的最小编辑次数,且最终位置恰好位于原点。

输入解题思路,AI测评打分。不知道怎么写?

首页