Tuesday, May 11, 2021

Published May 11, 2021 by Anonymous with 0 comment

Minimum prefixes required to be flipped to convert a Binary String to another

#include <bits/stdc++.h>

using namespace std;

  

void findOperations(string A,

                    string B, int N)

{

    

    

    int operations = 0;

  

    

    

    vector<int> ops;

  

    

    

    bool invert = false;

  

    

    for (int i = N - 1; i >= 0; i--) {

  

        

        

        if (A[i] != B[i]) {

  

            

            if (!invert) {

  

                

                

                operations++;

  

                

                

                ops.push_back(i + 1);

  

                

                invert = true;

            }

        }

  

        else {

  

            

            if (invert) {

  

                

                

                operations++;

  

                

                

                ops.push_back(i + 1);

  

                

                invert = false;

            }

        }

    }

  

    

    

    cout << operations << endl;

  

    

    

    if (operations != 0) {

        for (auto x : ops)

            cout << x << " ";

    }

}

  

int main()

{

    

    string A = "001", B = "000";

    int N = A.size();

  

    findOperations(A, B, N);

  

    return 0;

}

Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment