0% found this document useful (0 votes)
2 views1 page

Out Put Code

The document contains a C++ program that defines a function to generate the lexicographically smallest string by modifying a given string based on specific rules involving the number of allowed changes (K). The main function reads multiple test cases, processes each case, and outputs the resulting smallest string. The algorithm iteratively checks and modifies the string to achieve the desired result while managing the allowed changes.

Uploaded by

Zaki Malick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Out Put Code

The document contains a C++ program that defines a function to generate the lexicographically smallest string by modifying a given string based on specific rules involving the number of allowed changes (K). The main function reads multiple test cases, processes each case, and outputs the resulting smallest string. The algorithm iteratively checks and modifies the string to achieve the desired result while managing the allowed changes.

Uploaded by

Zaki Malick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

#include <string>
#include <algorithm>

std::string getLexicographicallySmallestString(int N, int K, std::string S) {


std::string result = S;
int i = 0;

while (K > 0 && i < N) {


if (result[i] == '0') {
if (K >= i) {
result[i] = '1';
K -= i;
} else {
result[i - K] = '1';
result[i] = '0';
K = 0;
}
}
i++;
}

return result;
}

int main() {
int T;
std::cin >> T;

for (int t = 0; t < T; t++) {


int N, K;
std::cin >> N >> K;

std::string S;
std::cin >> S;

std::string result = getLexicographicallySmallestString(N, K, S);


std::cout << result << std::endl;
}

return 0;
}

You might also like