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

0
7
Adv1


Adv2

#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.measurement();

  

    

    

    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 measurement = t.measurement();

  

    string dec = t;

  

    string Max = "";

  

    

    string t1 = "", t2 = "";

  

    

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

        if (j < measurement) {

            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.measurement(); i++) {

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

            Idx = i;

            break;

        }

    }

  

    if (Idx == -1) {

        return "0";

    }

  

    

    return ans.substr(Idx);

}

  

string resolve(string s, int Okay)

{

  

    int i = 0;

  

    whereas (i < Okay) {

  

        

        

        s = maxValue(s);

  

        i++;

    }

  

    return s;

}

  

int predominant()

{

  

    string s = "1010010";

    int Okay = 2;

  

    

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

  

    return 0;

}

Adv3