Queries for the Array (Codeforces 1861C)
Tags: Implement, strings

Monocarp had an array $$$a$$$ consisting of integers. Initially, this array was empty.Monocarp performed three types of queries to this array:  choose an integer and append it to the end of the array. Each time Monocarp performed a query of this type, he wrote out a character +;  remove the last element from the array. Each time Monocarp performed a query of this type, he wrote out a character -. Monocarp never performed this query on an empty array;  check if the array is sorted in non-descending order, i.,e. $$$a_1 \le a_2 \le \dots \le a_k$$$, where $$$k$$$ is the number of elements in the array currently. Every array with less than $$$2$$$ elements is considered sorted. If the array was sorted by the time Monocarp was performing that query, he wrote out a character 1. Otherwise, he wrote out a character 0. You are given a sequence $$$s$$$ of $$$q$$$ characters 0, 1, + and/or -. These are the characters that were written out by Monocarp, given in the exact order he wrote them out.You have to check if this sequence is consistent, i. e. it was possible for Monocarp to perform the queries so that the sequence of characters he wrote out is exactly $$$s$$$.

Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$). This string consists of characters 0, 1, + and/or -. This is the sequence of characters written by Monocarp, in the order he wrote them.Additional constraints on the input:   for every prefix of $$$s$$$, the number of characters + on it is not less than the number of characters - on it. In other words, if Monocarp actually performed these queries, he would never try to remove the last element from the empty array;  the sum of $$$|s|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. 

Output: For each test case, print YES if it was possible for Monocarp to perform the queries so that the sequence of characters he wrote is exactly $$$s$$$. Otherwise, print NO.You can print each letter in any register.

Note: In the first test case, Monocarp could perform the following sequence of queries:  add the integer $$$13$$$;  add the integer $$$37$$$;  check that the current array $$$[13, 37]$$$ is sorted in non-descending order (and it is sorted). In the fifth test case, Monocarp could perform the following sequence of queries:  add the integer $$$3$$$;  add the integer $$$2$$$;  check that the current array $$$[3, 2]$$$ is sorted (it is not);  remove the last element;  add the integer $$$3$$$;  check that the current array $$$[3, 3]$$$ is sorted (it is);  remove the last element;  add the integer $$$-5$$$;  check that the current array $$$[3, -5]$$$ is sorted (it is not). In all other test cases of the example test, it is impossible for Monocarp to write the sequence $$$s$$$ when performing the queries according to the statement.

Sample Inputs:
7
++1
+++1--0
+0
0
++0-+1-+0
++0+-1+-0
+1-+0

Sample Outputs:
YES
NO
NO
NO
YES
NO
NO

Solution (Python):
def solve():
    st = []
    for c in input():
        if c == '+':
            if st and st[-1] == -1:
                st.append(-1)
            else:
                st.append(0)
        elif c == '-':
            if st.pop() == 1 and st:
                st[-1] = 1
        elif c == '1':
            if len(st) >= 2:
                if st[-1] == -1:
                    return 'NO'
                st[-1] = 1
        else:
            if len(st) < 2 or st[-1] == 1:
                return 'NO'
            st[-1] = -1
    return 'YES'
print('\n'.join((solve() for _ in range(int(input())))))

Solution (Python):
import sys
input = sys.stdin.readline

def solve():
    impossible = False
    ar = input().strip()
    (maxSorted, minUnsorted, length) = (-1, -1, 0)
    for a in ar:
        if a == '+':
            length += 1
        elif a == '-':
            length -= 1
        elif a == '1':
            maxSorted = length
        elif a == '0':
            if minUnsorted == -1:
                minUnsorted = length
        if length < maxSorted:
            maxSorted = length
        if length < minUnsorted:
            minUnsorted = -1
        if a == '0' and length < 2:
            impossible = True
            break
        if a == '0' and length == maxSorted:
            impossible = True
            break
        if a == '1' and minUnsorted != -1 and (minUnsorted <= maxSorted):
            impossible = True
            break
    print('NO' if impossible else 'YES')
T = int(input())
for _ in range(T):
    solve()

Solution (Python):
from sys import stdin

def input():
    return stdin.readline()[:-1]
for _ in range(int(input())):
    s = input()
    mn = 0
    mx = 0
    c = 0
    for i in s:
        if i == '+':
            if mx == c:
                mx += 1
            c += 1
        elif i == '-':
            c -= 1
            mn = mn if mn < c else c
            mx = mx if mx < c else c
        elif i == '1':
            if c > mx:
                print('NO')
                break
            mx = mn = c
        else:
            if c < 2 or c <= mn:
                print('NO')
                break
            mx = c - 1 if c - 1 < mx else mx
    else:
        print('YES')