StrangeComputer
SRM 450 · 2009-10-17 · by Rydberg
SRM 450 · 2009-10-17 · by Rydberg · Greedy
Problem Statement
Problem Statement
You have been given a very strange computer. Its memory consists of several bits, each initially set to 0, and it can only perform the following type of operation:
Choose one of the bits in memory, and choose a value - 0 or 1. All the bits between the selected bit and the last bit in memory, inclusive, will be set to the chosen value. For example, if the memory is "0010", and you choose the second bit and a value of 1, the memory will change to "0111".
You are given aString mem. The number of characters in mem is equal to the number of bits in the computer's memory. Return the minimum number of operations required to set the computer's memory equal to mem.
Choose one of the bits in memory, and choose a value - 0 or 1. All the bits between the selected bit and the last bit in memory, inclusive, will be set to the chosen value. For example, if the memory is "0010", and you choose the second bit and a value of 1, the memory will change to "0111".
You are given a
Constraints
- mem will contain between 1 and 50 characters, inclusive.
- mem will contain only the characters '0' (zero) or '1' (one).
Examples
0)
"0011" Returns: 1
Choose the third bit and a value of 1.
1)
"000" Returns: 0
No operations are needed.
2)
"0100" Returns: 2
0000 -> 0111 -> 0100
3)
"111000111" Returns: 3
4)
"0" Returns: 0
Submissions are judged against all 74 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class StrangeComputer with a public method int setMemory(string mem) · 74 test cases · 2 s / 256 MB per case