1216A Prefixes

For a long time I did not do the topic, just yesterday there was a time not later come to practice.

Question is intended: to a string of length n, let a string of even length a, b are equal number, that is, each two consecutive characters a, b each have one number. Now given this string of characters, so that you become a string consistent with this law, you can arbitrarily change the character, put a into b or b into a, minimum output frequency change, and then change the output string.

Solution: very clear, scratch through each of the two characters, determining whether a = b is equal, not equal to meet the requirements, the number of change of + 1, wherein a replacement character into another character.

c++:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,ans=0;
    string s;
    cin>>n>>s;
    for(int i=0; i<n; i+=2)
    {
        if(s[i]==s[i+1])
        {
            ans++;
            if(s[i]=='a') s[i]+=1;
            else s[i]-=1;
        }
    }
    cout<<ans<<endl<<s<<endl;
    return 0;
}

 

Published 395 original articles · won praise 126 · Views 200,000 +

Guess you like

Origin blog.csdn.net/memory_qianxiao/article/details/101145440