Saturday, December 3, 2022
HomeSoftware DevelopmentMaximize worth of Binary String in Okay steps by performing XOR of...

Maximize worth of Binary String in Okay steps by performing XOR of Substrings


#embrace <bits/stdc++.h>

#outline ll lengthy lengthy

utilizing namespace std;

  

void addZeros(string& str, int n)

{

    for (int i = 0; i < n; i++) {

        str = "0" + str;

    }

}

  

string getXOR(string a, string b)

{

  

    

    int aLen = a.size();

    int bLen = b.size();

  

    

    

    if (aLen > bLen) {

        addZeros(b, aLen - bLen);

    }

    else if (bLen > aLen) {

        addZeros(a, bLen - aLen);

    }

  

    

    int len = max(aLen, bLen);

  

    

    string res = "";

    for (int i = 0; i < len; i++) {

        if (a[i] == b[i])

            res += "0";

        else

            res += "1";

    }

  

    return res;

}

  

string maxValue(string S)

{

  

    

    ll n = S.dimension();

  

    

    

    bool f = 1;

  

    ll Idx, Count0 = 0, Count1 = 0;

  

    

    

    

  

    for (ll i = 0; i < n; i++) {

  

        

        if (S[i] == '1') {

  

            Count1++;

        }

        

        else {

  

            if (f) {

                

                

                Idx = i;

  

                f = 0;

            }

            Count0++;

        }

    }

  

    

    if (Count1 == n) {

        return getXOR(S, "1");

    }

  

    

    

    if (Count0 == n) {

        return "0";

    }

  

    

    string t = S.substr(Idx, n - Idx);

  

    

    ll dimension = t.dimension();

  

    string dec = t;

  

    string Max = "";

  

    

    string t1 = "", t2 = "";

  

    

    for (ll j = 0; j < n; j++) {

        if (j < dimension) {

            t1 += S[j];

        }

        else {

  

            string dec1 = t1;

  

            

            

            string res = getXOR(dec, dec1);

  

            if (res > Max) {

                Max = res;

                t2 = t1;

            }

  

            t1 = t1.substr(1);

            t1 += S[j];

        }

    }

  

    string dec1 = t1;

  

    string res = getXOR(dec1, dec);

    if (res > Max) {

        Max = res;

        t2 = t;

    }

  

    

    string d1 = S;

    string d2 = t2;

  

    

    string ans = getXOR(d1, d2);

  

    Idx = -1;

  

    

    for (ll i = 0; i < ans.dimension(); i++) {

        if (ans[i] != '0') {

            Idx = i;

            break;

        }

    }

  

    if (Idx == -1) {

        return "0";

    }

  

    

    return ans.substr(Idx);

}

  

string remedy(string s, int Okay)

{

  

    int i = 0;

  

    whereas (i < Okay) {

  

        

        

        s = maxValue(s);

  

        i++;

    }

  

    return s;

}

  

int major()

{

  

    string s = "1010010";

    int Okay = 2;

  

    

    cout << remedy(s, Okay) << endl;

  

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments