K-th good string
Time limit2sMemory limit512 MB
Given a bracket string S, list the distinct good strings that appear as subsequences in lexicographic order and print the K-th one.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String, Combinatorics
- Solved
- No attempts yet
Problem
Good strings are defined as follows.
- The string
()is a good string. - If is a good string, then
(SS...S)is also a good string. In other words, if you write one good string one or more times in a row and wrap the whole result in parentheses, you get a good string. - No other string is a good string.
A subsequence of a string is a string obtained by deleting zero or more characters from .
You are given a string . Each character of is ( or ).
Let be the set of distinct good strings that are subsequences of . Because is a set, a good string that occurs as a subsequence several times is contained in only once. For example, if = (()()), then contains (), (()), and (()()).
Given , write a program that finds the -th string of in lexicographic order. Indexing starts at 1. In lexicographic comparison, ( comes before ).
Input
The first line contains the string . The length of is between 1 and 150, inclusive.
The second line contains the integer ().
Output
Print the -th string of in lexicographic order. If no such string exists, print -1.