zoukankan      html  css  js  c++  java
  • codeforces 712B B. Memory and Trident(水题)

    题目链接:

    B. Memory and Trident

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    input
    RRU
    output
    -1
    input
    UDUR
    output
    1
    input
    RUUR
    output
    2

    题意:

    问最少改变多少个才能最后回到原点;

    思路:

    水水水;

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    #include <map>
      
    using namespace std;
      
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
      
    typedef  long long LL;
      
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
      
    const int mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=(1<<20)+10;
    const int maxn=1e5+110;
    const double eps=1e-12;
     
    
    char s[maxn];
    int main()
    {
        scanf("%s",s);
        int len=strlen(s),l=0,r=0,u=0,d=0;
        for(int i=0;i<len;i++)
        {
            if(s[i]=='R')r++;
            else if(s[i]=='L')l++;
            else if(s[i]=='U')u++;
            else d++;
        }
        if(len&1)cout<<"-1
    ";
        else cout<<(abs(l-r)+abs(u-d))/2<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    MySQL Explain学习笔记
    postman测试文件上传接口教程
    URLDecoder异常Illegal hex characters in escape (%)
    第三章 jQuery总结 参考文本
    史上最简单的一道面试题!坑人吧
    cookie和session的关联关系
    服务器配置
    Rancher 添加主机无法显示、添加主机无效的解决办法
    .NET Core 跨平台 串口通讯 ,Windows/Linux 串口通讯,flyfire.CustomSerialPort 的使用
    .Net Core 跨平台应用使用串口、串口通信 ,可能出现的问题、更简洁的实现方法
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5876903.html
Copyright © 2011-2022 走看看