Codeforces Problem 712B Memory and Trident(implementation)

本文解析了Codeforces 712B题目的解决方案,介绍了一个角色Memory如何通过最少的指令修改从任意起点返回原点的问题。讨论了实现细节,并给出源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此文章可以使用目录功能哟↑(点击上方[+])

比赛链接→Codeforces Round #370 (Div. 2)

 Codeforces Problem 712B Memory and Trident

Accept: 0    Submit: 0
Time Limit: 2 seconds    Memory Limit : 256 megabytes

 Problem Description

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.

 Input

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

 Output

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.

 Sample Input

RRU
UDUR
RUUR

 Sample Output

-1
1
2

 Hint

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.

 Problem Idea

解题思路:

【题意】
Memory从二维坐标系的原点出发,按字符串s的指示运动

R:向右;L:向左;U:向上;D:向下

Memory最终想回到原点,问至少需要改变字符串s中的几个字符

若无论如何改变都无法回到原点,输出"-1"


【类型】
implementation

【分析】

很显然的,Memory从原点出发想要回到原点

那么他向右走的次数与向左走的次数需要一样,同时,向上走的次数和向下走的次数也必须一样

这也就是说,向右、向左、向上、向下走的总次数必定是偶数次

所以若字符串s长度为奇数,显然输出"-1"

接着将向右走的次数与向左走的次数抵消,向上走的次数与向下走的次数抵消

剩下的就只能用水平(向左、向右)的次数抵垂直(向上、向下)的次数,或垂直的次数抵水平的次数才能回到原点

而这部分就是需要改变的字符数

【时间复杂度&&优化】
O(strlen(s))

题目链接→Codeforces Problem 712B Memory and Trident

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 100005;
const int M = 20005;
const int inf = 1000000007;
const int mod = 7;
char s[N];
int main()
{
    int v=0,h=0,i;
    scanf("%s",s);
    if(strlen(s)%2)
    {
        puts("-1");
        return 0;
    }
    for(i=0;s[i]!='\0';i++)
        if(s[i]=='U')
            v++;
        else if(s[i]=='D')
            v--;
        else if(s[i]=='R')
            h++;
        else
            h--;
    printf("%d\n",(abs(v)+abs(h))/2);
    return 0;
}
菜鸟成长记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值