zoukankan      html  css  js  c++  java
  • Codeforces Round #587 (Div. 3) A. Prefixes

    链接:

    https://codeforces.com/contest/1216/problem/A

    题意:

    Nikolay got a string s of even length n, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from 1 to n.

    He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.

    The prefix of string s of length l (1≤l≤n) is a string s[1..l].

    For example, for the string s="abba" there are two prefixes of the even length. The first is s[1…2]="ab" and the second s[1…4]="abba". Both of them have the same number of 'a' and 'b'.

    Your task is to calculate the minimum number of operations Nikolay has to perform with the string s to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.

    思路:

    从开头开始每两个字符需要不同, 搞一下就行了.

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 2e5+10;
    
    int n;
    char s[MAXN];
    
    int main()
    {
        cin >> n;
        cin >> s;
        int op = 0, a = 0, b = 0;
        for (int i = 0;i < n;i+=2)
        {
            if (s[i] != s[i+1])
                continue;
            if (s[i] == 'a')
                s[i] = 'b';
            else
                s[i] = 'a';
            op++;
        }
        cout << op << endl;
        cout << s << endl;
    
        return 0;
    }
    
  • 相关阅读:
    IntelliJ IDEA 2020.1.1中java web项目的配置
    Js查漏补缺10-数组、栈、队列、回调函数等
    Js查漏补缺09-this对象
    Js查漏补缺08-闭包
    Js查漏补缺07-匿名函数应用到的框架
    Js查漏补缺06-匿名函数的用法
    Js查漏补缺05-函数
    Js查漏补缺04-Object类型
    Js查漏补缺03-循环结构
    Runnabler
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11622744.html
Copyright © 2011-2022 走看看