Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-09-11 23:53:07
forward
1095 people have browsed it

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
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!