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

    原文链接:https://www.cnblogs.com/xwl3109377858/p/11564167.html

    Codeforces Round #587 (Div. 3)

    A - Prefixes

    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'.

    Input

    The first line of the input contains one even integer n (2≤n≤2⋅105) — the length of string s.

    The second line of the input contains the string s of length n, which consists only of lowercase Latin letters 'a' and 'b'.

    Output

    In the first line print the minimum number of operations Nikolay has to perform with the string s to modify it so that every its prefix of evenlength has an equal amount of letters 'a' and 'b'.

    In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.

    Examples

    input

    4

    bbbb

    output

    2

    abba

    input

    6

    ababab

    output

    0

    ababab

    input

    2

    aa

    output

    1

    ba

    Note

    In the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'.

    In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'.

     

    题意:题目意思大概就是给你一个由 'a'、'b' 构成的字符串,问你最少改变几个字符

    使这个字符串的每个偶数前缀的 'a'、'b' 字符数量相同,并输出改变后的字符串。

    思路:直接扫一遍,每次找两个字符,如果数量不同就改。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<map>
     7 #include<set>
     8 #include<vector>
     9 #include<queue>
    10 #include<list>
    11 #include<stack>
    12 using namespace std;
    13 #define ll long long 
    14 const int mod=1e9+7;
    15 const int inf=1e9+7;
    16  
    17 //const int maxn=
    18  
    19 int main()
    20 {
    21     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    22     
    23     int n;
    24     cin>>n;
    25     string str;
    26     cin>>str;
    27     
    28     int cnt_a=0,cnt_b=0;
    29     
    30     int ans=0;
    31     
    32     for(int i=0;i<n;i++)
    33     {
    34         if(str[i]=='a')
    35             cnt_a++;
    36         else if(str[i]=='b')
    37             cnt_b++;
    38         if(i&1)
    39         {
    40             if(cnt_a!=cnt_b)
    41             {
    42                 ans++;
    43                 if(cnt_a==2)
    44                     str[i]='b';
    45                 else if(cnt_b==2)
    46                     str[i]='a';    
    47             }
    48             cnt_a=0;
    49             cnt_b=0;
    50         }
    51     }
    52     
    53     cout<<ans<<endl;
    54     cout<<str<<endl;
    55     
    56     return 0;
    57 }
    大佬见笑,,
  • 相关阅读:
    职业的选择
    事务的隔离性理解
    浅谈CSS和JQuery实现鼠标悬浮图片放大效果
    jvm 类加载器
    jvm 类加载
    Spring 启动 Bean加载流程
    优雅的博客园客户端发布Forms版啦。
    Xamarin Forms中WebView的自适应高度
    2017回顾与2018目标
    优雅的博客园Android客户端
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/11564167.html
Copyright © 2011-2022 走看看