Brackets
Time limit1sMemory limit128 MB
Given n and k, print the k-th correct bracket sequence of length 2n in lexicographic order.
- Level
Medium6 of 10
- Topics
- Combinatorics, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
A correct bracketing is a string of the brackets ( and ) in which the number of opening brackets equals the number of closing brackets, and every prefix contains at least as many opening brackets as closing brackets. For example, ()() is a correct bracketing, while ())( is not, because its prefix ()) has more closing brackets than opening brackets.
Given two correct bracketings of length , the earlier one is the bracketing that has an opening bracket at the first position where the two strings differ. This ordering is exactly lexicographical order when ( is treated as smaller than ).
Write a program that reads two integers and , then finds and prints the lexicographically -th correct bracketing of length (bracketings are numbered starting from ).
Input
A single line with two integers and (, ), separated by one space.
Output
Print the correct bracketing of length that ranks -th among all correct bracketings of length in lexicographical order. The input is guaranteed to be chosen so that this bracketing always exists.