ParenthesesDiv1Easy
SRM 688 · 2016-04-01 · by cgy4ever
Problem Statement
- The empty string "" is a correct sequence.
- If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
- If "X" is a correct sequence, then "(X)" is a correct sequence.
- Each correct parentheses sequence can be derived using the above rules.
You have a
You can only change the string using magic flips. In order to do a magic flip, you specify two 0-based indices L and R such that L <= R. The operation modifies the characters on indices from L to R, inclusive. First, the order of these characters is reversed. Then, each character is toggled to the opposite one. That is, each '(' in the specified range changes to a ')' and vice versa.
For example, suppose you have the string "((()". If you do a magic flip on the entire string (i.e., choosing L=0 and R=3), it first gets reversed to ")(((" and then all parentheses are toggled. Thus, flipping the string "((()" produces the string "()))".
Given s, find and report any sequence of at most 10 magic flips that changes s into a correct parentheses sequence. If there is no such sequence of magic flips, return the
Constraints
- s will contain between 1 and 1,000 characters, inclusive.
- Each character in s will be '(' or ')'.
")("
Returns: {0, 0, 1, 1 }
The first flip has L=R=0. It changes the string to "((". The second flip has L=R=1. It changes that string to "()", which is a correct parentheses sequence.
"))))))(((((("
Returns: {0, 5, 6, 11 }
Here, the first flip changes the string to "((((((((((((" and then the second flip changes it to "(((((())))))". Note that {0,0,1,1,2,2,...,11,11} is not a valid solution. This sequence of flips produces a correct parentheses sequence (by flipping each character separately). However, this sequence consists of 12 flips and we are only allowed to perform at most 10 flips.
"))()())()"
Returns: {-1 }
This s has length 9. There is no correct parentheses sequence of length 9, so there can be no good sequence of flips.
")()((("
Returns: {0, 0, 3, 3, 5, 5 }
"()"
Returns: { }
This time we don't need to do anything.
Submissions are judged against all 118 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ParenthesesDiv1Easy with a public method vector<int> correct(string s) · 118 test cases · 2 s / 256 MB per case