[蓝桥杯][Previous Test Questions] Flip the coin (greedy)

Insert picture description here
Idea: Because you have to flip the two adjacent ones each time, the least case is to find the difference between the two different positions of the two strings from the beginning, which is the number of times to flip.

#include <bits/stdc++.h>

using namespace std;
int main()
{
    
    
    //ios::sync_with_stdio(false);
    string a,b; cin>>a>>b;
    int len1 = a.length();
    int flag  = 0,ans = 0;
    for(int i=0;i<len1;i++){
    
    
        if(a[i]!=b[i]){
    
    
            int x;
            if(flag==0){
    
    
                x = i; flag = 1;
            }
            else {
    
    
                ans+=(i-x); flag = 0;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_43811879/article/details/107890158