Codeforces Round #587 (Div. 3)

Table of Contents

  • A. Prefixes
    • 题目链接:https://codeforces.com/contest/1216/problem/A
    • 题目大意:
    • 解题思路:

A. Prefixes

题目链接:https://codeforces.com/contest/1216/problem/A

题目大意:

(input)输入n,表示下一行输入的字符串的长度,输入str

通过最少的操作数来达到每偶数段(1~2*n)a的个数和b的个数是相等

解题思路:

简单的模拟,每次维护长度为2的区间,如果在这区间中,两个相同那么必定要进行一次操作cnt++,改s[i]或s[i+1]无区别,故所有偶数区间都能满足

#include 
#define REP(i, a, b) for(int i = a; i < b; i++)
#define REP_(i, a, b) for(int i = a; i <= b; i++)
#define sl(n) scanf("%lld", &n);
#define si(n) scanf("%d", &n);
#define RepAll(a) for(auto x: a)
#define cout(ans) cout << ans << endl;
typedef long long ll;

using namespace std;
int main(){
    ll n, cnt = 0;
    sl(n);
    string s;
    cin >> s;
    for(int i = 0; i < s.size() -  1; i+=2){
        //每两位每两位的检查
        if(s[i] == s[i + 1]){
            cnt++;
            s[i + 1] == 'a' ? s[i+1] = 'b' : s[i+1] = 'a';
        }
    }
    cout << cnt << '\n' << s << endl;
}

 

你可能感兴趣的:(Codeforces Round #587 (Div. 3))