Atcoder Regular Contest(ARC) 136 A

A - A ↔ BBAtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.https://atcoder.jp/contests/arc136/tasks/arc136_a

目录

题面

题解

代码


题面

题面意思大概是,给你一个字符串(包含‘A’‘B’‘C’),

如果你遇到“BB”,可以把它换成‘A’ 

如果你遇到‘A’,可以把它换成“BB”

你可以对以上操作做无限次

问你如何把他改成字典序最小的字符串.

题解

思考一下,你会发现:

1. 如果你遇到“BB”,可以把它换成第一个字符换成‘A’,把后面一个换成‘A’‘B’‘C’除外的字符.仔细思考,你会发现这对后面答案没有影响.

2. 如果你遇到“BA”,可以把它换成“AB”.

if (s[i] == 'B'){
    if (s[i + 1] == 'B') cout << 'A',s[i + 1] = '_';
    else if (s[i + 1] == 'A') cout << 'A',s[i + 1] = 'B';
    else cout << s[i];
}

3. 遇到‘A’‘C’,直接输出.

4. 遇到你换出来的字符,跳过.

if (s[i] == 'C' || s[i] == 'A') cout << s[i];
if (s[i] == '_') continue;

以下代码,我就用‘_’代表换下来的字符:

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1005
#define N 500005
#define debug(...) cout << '[' << 'D' << 'E' << ']' << __LINE__ << ':' << __VA_ARGS__ << endl;
#define v(x) #x << '(' << x << ')'
#define mod 998244353
#define Mod (int) 1e9 + 7
#define who(s,limit,tpe) s + tpe , s + limit + tpe
#define all(s) s.begin(),s.end()
#define s_(s) s.size()
int a[N];
string s;
int main(){
    int n,m;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> s;
    for(int i = 0 ; i < n - 1 ; i ++) {
        if (s[i] == 'C' || s[i] == 'A') cout << s[i];
        if (s[i] == '_') continue;
        if (s[i] == 'B'){
            if (s[i + 1] == 'B') cout << 'A',s[i + 1] = '_';
            else if (s[i + 1] == 'A') cout << 'A',s[i + 1] = 'B';
            else cout << s[i];
        }
    }
    if (s[n - 1] != '_') cout << s[n - 1] << endl ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值