Kuroni and Simple Strings (Codeforces 1305B)
Tags: Greedy, two pointers

Now that Kuroni has reached 10 years old, he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!We say that a string formed by $$$n$$$ characters '(' or ')' is simple if its length $$$n$$$ is even and positive, its first $$$\frac{n}{2}$$$ characters are '(', and its last $$$\frac{n}{2}$$$ characters are ')'. For example, the strings () and (()) are simple, while the strings )( and ()() are not simple.Kuroni will be given a string formed by characters '(' and ')' (the given string is not necessarily simple). An operation consists of choosing a subsequence of the characters of the string that forms a simple string and removing all the characters of this subsequence from the string. Note that this subsequence doesn't have to be continuous. For example, he can apply the operation to the string ')()(()))', to choose a subsequence of bold characters, as it forms a simple string '(())', delete these bold characters from the string and to get '))()'. Kuroni has to perform the minimum possible number of operations on the string, in such a way that no more operations can be performed on the remaining string. The resulting string does not have to be empty.Since the given string is too large, Kuroni is unable to figure out how to minimize the number of operations. Can you help him do it instead?A sequence of characters $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters.

Input: The only line of input contains a string $$$s$$$ ($$$1 \le |s| \le 1000$$$) formed by characters '(' and ')', where $$$|s|$$$ is the length of $$$s$$$.

Output: In the first line, print an integer $$$k$$$  — the minimum number of operations you have to apply. Then, print $$$2k$$$ lines describing the operations in the following format:For each operation, print a line containing an integer $$$m$$$  — the number of characters in the subsequence you will remove.Then, print a line containing $$$m$$$ integers $$$1 \le a_1 < a_2 < \dots < a_m$$$  — the indices of the characters you will remove. All integers must be less than or equal to the length of the current string, and the corresponding subsequence must form a simple string.If there are multiple valid sequences of operations with the smallest $$$k$$$, you may print any of them.

Note: In the first sample, the string is '(()(('. The operation described corresponds to deleting the bolded subsequence. The resulting string is '(((', and no more operations can be performed on it. Another valid answer is choosing indices $$$2$$$ and $$$3$$$, which results in the same final string.In the second sample, it is already impossible to perform any operations.

Sample Inputs:
(()((

)(

(()())

Sample Outputs:
1
2
1 3

0

1
4
1 2 5 6

Solution (Python):
from collections import *
from itertools import *
from bisect import *
from heapq import *
from math import ceil, prod, gcd, factorial
from time import time
mod = 10 ** 9 + 7
d = ((1, 0), (0, 1), (0, -1), (-1, 0))
alpha = 'abcdefghijklmnopqrstuvwxyz'

def helper(i, j, total):
    return total + size * c
t = 1
for _ in range(t):
    s = list(input())
    res = []
    while True:
        temp = []
        i = 0
        j = len(s) - 1
        while i < j:
            while i < j and s[i] == ')':
                i += 1
            while i < j and s[j] == '(':
                j -= 1
            if i < j:
                temp.append(i + 1)
                temp.append(j + 1)
                i += 1
                j -= 1
        temp.sort()
        if temp:
            res.append(temp)
            for i in temp[::-1]:
                s.pop(i - 1)
        else:
            break
    print(len(res))
    for item in res:
        print(len(item))
        print(*item)

Solution (Python):
s = input()
n = len(s)
i = 0
j = n - 1
x = [0] * n
c = 0
a = []
b = []
while i < j:
    while i < n and s[i] != '(':
        i += 1
    while j >= 0 and s[j] != ')':
        j -= 1
    if i < j:
        a.append(i + 1)
        b.append(j + 1)
        c += 1
    else:
        break
    i += 1
    j -= 1
if len(a) > 0:
    print(1)
    print(len(a) * 2)
    for i in a:
        print(i, end=' ')
    for i in range(len(b)):
        print(b[len(b) - i - 1], end=' ')
else:
    print(0)

Solution (Python):
s = input()
ob = []
cb = []
n = len(s)
for i in range(n):
    if s[i] == '(':
        ob.append(i)
    if s[i] == ')':
        cb.append(i)
ol = 0
cl = len(cb) - 1
new = []
while ol < len(ob) and cl >= 0:
    if ob[ol] > cb[cl]:
        break
    else:
        new.append(ob[ol])
        new.append(cb[cl])
    ol += 1
    cl -= 1
new.sort()
if len(new) == 0:
    print(0)
else:
    print('1')
    print(len(new))
    for val in new:
        print(val + 1, end=' ')