Home  >  Article  >  Backend Development  >  Arrange a binary string to obtain the maximum value within the index range. C/C++?

Arrange a binary string to obtain the maximum value within the index range. C/C++?

WBOY
WBOYforward
2023-09-11 23:53:071219browse

Arrange a binary string to obtain the maximum value within the index range. C/C++?

For a given string consisting of 0 and 1, we are given M disjoint ranges A, B (A

The activity is to find a legal or valid permutation that satisfies the following two conditions -

  • The sum of all M numbers between the given ranges is the largest.

  • The string will be the lexicographically largest. String 1100 has a higher lexicographic order than string 1001.

Example

Input
11100
3
3 4
5 5
Output
00111
First we put 1’s in position 3 and 4 then in 5 as there are no 1’s left, the string formed is 00111.
Input
0000111
2
1 1
1 2
Output
1110000

In the above example, we first put 1 in the 1st and 2nd positions and then there is another '1' left,

So, we use this to maximize the string in lexicographic order and put it in the 3rd position, thus completing the rearrangement.

The above is the detailed content of Arrange a binary string to obtain the maximum value within the index range. C/C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete