Bracket Strings
Time limit2sMemory limit128 MB
Given N and K, find the K-th lexicographically smallest string of length N over '(' and ')' that is not a valid bracket string, using combinatorial counting.
- Level
Medium7 of 10
- Topics
- Combinatorics, Dynamic programming, Math, Greedy
- Solved
- No attempts yet
Problem
A bracket string is defined as follows.
- The empty string is a bracket string.
- If
Sis a bracket string, then(S)is also a bracket string. - If
SandTare bracket strings, thenSTis also a bracket string. - Every bracket string can be built using only the three rules above.
In other words, a bracket string is a string in which every opening bracket is correctly matched with a closing bracket.
This problem concerns strings that consist only of ( and ) but are not bracket strings. Call such a string a non-bracket string.
Write a program that prints the -th (0-indexed) non-bracket string of length in lexicographic order. If no such string exists, print -1. In lexicographic order, ( comes before ). The lexicographically smallest string is the -th string.
Input
The first line contains two integers and , separated by a space.
Output
Print the answer string on the first line. If no string satisfies the condition, print -1.