Ivan Smirnov
시간 제한2초메모리 제한512 MB
런 렝스 인코딩된 두 괄호 문자열이 주어질 때, 두 문자열을 교차시켜 올바른 괄호 문자열을 만들 수 있는지 판별한다.
문제
https://stackoverflow.com/questions/45658828/number-of-ways-to-merge-2-parenthesis-sequences. Now you can solve a strictly harder problem in about years. This almost fits in the time limit. All you have to do is some minor optimizations.
Two sequences of parentheses and mergeable if they can be interleaved to form a balanced parentheses sequence. Formally, if has length and has length they are mergeable if and only if there exists a balanced parentheses sequence of length , and two disjoint sequences of indices and of length and respectively, such that:
- For all
- For all
The RLE (Run Length Encoding) of some string is a list of pairs where is a character and is a positive integer and , the length of the encoding (i.e. number of pairs in the list) is called the run length of the string. The original string of RLE consists of repetitions of character , followed by repetitions of character , and so on, and ends with repetitions of character . It can be shown that RLE is unique.
You are given an RLE of a parentheses sequence and RLEs of other parentheses sequences candidates .
For each find out if and are mergeable.
입력
The input starts with a description of the sequence . The first line contains a single integer (), the run length of .
lines follow. -th of them describes a single pair in the RLE of and contains a character and an integer separated by a single space ().
The next line contains a single integer (), the number of sequences to check.
The remaining lines describe those sequences one by one, using the same format as the description of . If this isn't clear you should take a look at the samples.
It is guaranteed that the sum of run lengths of those sequences doesn't exceed .
출력
Print lines.
The -th of those lines should contain 1 if and are mergeable and 0 if they are not.