Balance brackets

5

1 votes
String Algorithms, C++, String Searching, Algorithms
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.

Given a string  of length  consisting of two characters (open bracket)  and (close bracket) . We need to decompose  into  consecutive non-empty substrings such that every character is present in only one substring.

Say substrings are . Choose an array , which is a permutation of  to  i.e. it is of size  and include every element from  to .

Also, along we can choose to keep every substring as it is or in reverse order. That means you can reverse  before form  new string 

Now, form a new string . A matching pair of brackets is not balanced if the set of brackets it encloses are not matched.

A string is said to be balanced if

  • Empty string is a balanced string.
  • if  is a balanced string, then  is also a balanced string.
  • if  and  are balanced strings, then  (concatenation of  and ) is also a balanced string.

For example : (())) is not balanced as last closing bracket is unmatched.

Suppose number of balanced substrings in  is  (of length greater than 1) and number of substrings among which were not reversed are .

Aim is to maximize .

Input format

  • First line contains two space-separated integers 
  • Next line contains a string .

Output format

  • In the first  lines, print  integers denoting end index of  substring (indexes are 1 based) and whether it is reversed or not (0 for not reversed and 1 for reversed).
  • In next line print  space separated integers, denoting array  (permutation of 1 to K)
  • Note : End index of substrings should be in increasing order.

Verdict and scoring

  • Your score is equal to the sum of the values of all test files. The value of each test file is equal to the value of  that you found.
  • If decomposition of string  is invalid, or array  is invalid you will receive a wrong answer verdict.

Constraints

 

Sample Input
4 2
)(()
Sample Output
1 0
4 0
2 1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Note: The sample provided is not a valid testcase as per the constraints, as N !=1000 and K does not lie in [50, 100]. Its just for simpler visualization.

  • S1 = )
  • S2 = (()

Since we choose not to reverse any of the substrings, they remain the same. 

  • V = S2 + S1
  • V = (())

Number of balanced substrings is 2 - () , (()).
Number of not reversed substrings is 2.

  • Z = 2 * (2 + 1) = 6
Editor Image

?