description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
l = input()
m = []
d = {(0): [], (1): []}
c = 0
for i in l:
a = int(i)
b = int(not a)
if len(d[b]) != 0:
p = d[b].pop()
m.append(p)
d[a].append(p)
else:
c += 1
m.append(c)
d[a].append(c)
print(c)
print(*m, sep=" ") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER LIST LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for i in range(t):
n = int(input())
l = input()
one = []
zero = []
d = {j: (0) for j in range(n)}
r = 0
for j in range(n):
if l[j] == "0":
if len(one) == 0:
r = r + 1
zero.append(r)
d[j] = r
else:
zero.append(one.pop())
d[j] = zero[-1]
if l[j] == "1":
if len(zero) == 0:
r = r + 1
one.append(r)
d[j] = r
else:
one.append(zero.pop())
d[j] = one[-1]
print(r)
for j in range(n):
print(d[j], end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
ls0, ls1 = [], []
ctr = 1
s = input()
an = 0
ans = ""
for i in range(n):
if s[i] == "1":
if len(ls0):
ls1.append(ls0[-1])
ls0.pop()
ans += str(ls1[-1]) + " "
else:
ls1.append(ctr)
ans += str(ctr) + " "
an = max(an, ctr)
ctr += 1
elif len(ls1):
ls0.append(ls1[-1])
ls1.pop()
ans += str(ls0[-1]) + " "
else:
ls0.append(ctr)
ans += str(ctr) + " "
an = max(an, ctr)
ctr += 1
print(an)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def func(s):
find_0 = []
find_1 = []
latest = 1
output = []
if s[0] == "0":
find_1.append(latest)
else:
find_0.append(latest)
latest += 1
output.append(1)
for i in s[1:]:
if i == "0":
if len(find_0) == 0:
find_1.append(latest)
output.append(latest)
latest += 1
else:
x = find_0[-1]
output.append(x)
find_1.append(x)
find_0.pop()
elif len(find_1) == 0:
find_0.append(latest)
output.append(latest)
latest += 1
else:
x = find_1[-1]
output.append(x)
find_0.append(x)
find_1.pop()
return str(latest - 1) + "\n" + " ".join(list(map(str, output)))
def main():
for i in range(int(input())):
input()
print(func(input()))
main() | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
a = input()
res, k, d, t = [-1] * n, 0, {(0): [], (1): []}, 2
for i in range(n):
cur = int(a[i])
alt = 1 - cur
if len(d[alt]) > 0:
t = d[alt].pop()
d[cur].append(t)
else:
k += 1
d[cur].append(k)
t = k
res[i] = t
print(k)
print(*res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER VAR NUMBER DICT NUMBER NUMBER LIST LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
_ = int(input())
s = input()
res = []
pos = [[], []]
for i in s:
nxt = len(pos[0]) + len(pos[1])
if i == "0":
if not len(pos[1]):
pos[0].append(nxt)
else:
nxt = pos[1][-1]
pos[0].append(nxt)
pos[1].pop()
elif not len(pos[0]):
pos[1].append(nxt)
else:
nxt = pos[0][-1]
pos[1].append(nxt)
pos[0].pop()
res.append(nxt + 1)
print(len(pos[0]) + len(pos[1]))
print(*res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST LIST LIST FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for testcases in range(int(input())):
num = int(input())
s = input()
ans = []
flags = {(0): [], (1): []}
box = 0
key = [0, 0]
for i in range(num):
if key[(int(s[i]) + 1) % 2] == 0:
box += 1
key[int(s[i])] += 1
flags[int(s[i])].append(box)
ans.append(box)
else:
key[int(s[i])] += 1
key[(int(s[i]) + 1) % 2] -= 1
flags[int(s[i])].append(flags[(int(s[i]) + 1) % 2][-1])
ans.append(flags[(int(s[i]) + 1) % 2].pop(-1))
print(box)
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT NUMBER NUMBER LIST LIST ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
def solve(n, s):
ans = []
a, b = [], []
tg = 0
for x in s:
if x == "0":
if not b:
tg += 1
a.append(tg)
else:
g = b.pop()
a.append(g)
ans.append(a[-1])
else:
if not a:
tg += 1
b.append(tg)
else:
g = a.pop()
b.append(g)
ans.append(b[-1])
return tg, ans
t = int(input())
for _ in range(t):
ln1 = next(sys.stdin)
n = int(ln1)
ln2 = next(sys.stdin)
s = ln2.strip()
k, a = solve(n, s)
print(k)
print(" ".join(map(str, a))) | IMPORT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def mi():
return map(int, input().split())
def ii():
return int(input())
def li():
return list(map(int, input().split()))
def si():
return input().split()
t = ii()
for _ in range(t):
n = ii()
a = input()
dic = {}
dic["0 0"] = 0
dic["1 1"] = 0
dic["1 0"] = 0
dic["0 1"] = 0
ans = [i for i in range(1, n + 1)]
cnt = 0
tmp1 = []
tmp2 = []
tmp3 = []
tmp4 = []
for i in range(n):
if a[i] == "1":
if dic.get("0 0") and dic["0 0"] > 0:
dic["0 0"] -= 1
ans[i] = tmp1.pop()
dic["0 1"] += 1
tmp4.append(ans[i])
elif dic.get("1 0") and dic["1 0"] > 0:
dic["1 1"] += 1
dic["1 0"] -= 1
ans[i] = tmp3.pop()
tmp2.append(ans[i])
else:
dic["1 1"] += 1
cnt += 1
ans[i] = cnt
tmp2.append(cnt)
elif dic.get("1 1") and dic["1 1"] > 0:
dic["1 1"] -= 1
ans[i] = tmp2.pop()
dic["1 0"] += 1
tmp3.append(ans[i])
elif dic.get("0 1") and dic["0 1"] > 0:
dic["0 0"] += 1
dic["0 1"] -= 1
ans[i] = tmp4.pop()
tmp1.append(ans[i])
else:
dic["0 0"] += 1
cnt += 1
tmp1.append(cnt)
ans[i] = cnt
print(cnt)
for i in ans:
print(i, end=" ")
print() | FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR STRING VAR STRING NUMBER VAR STRING NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR STRING VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING VAR STRING NUMBER VAR STRING NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR STRING VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
s = input()
cnt = 1
end = {(0): [-1], (1): [-1]}
end[int(s[0])].append(0)
ans = [cnt]
for i in range(1, n):
is_val = int(s[i])
not_val = is_val ^ 1
if end[not_val][-1] != -1:
end[is_val].append(end[not_val].pop())
ans.append(ans[end[is_val][-1]])
else:
end[is_val].append(i)
ans.append(cnt + 1)
cnt += 1
print(cnt)
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER LIST NUMBER LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
while t:
n = int(input())
s = input()
ans = []
m = -1
pos0 = []
pos1 = []
for i in s:
newpos = len(pos0) + len(pos1) + 1
if i == "0":
if len(pos1) == 0:
pos0.append(newpos)
else:
newpos = pos1.pop(len(pos1) - 1)
pos0.append(newpos)
elif len(pos0) == 0:
pos1.append(newpos)
else:
newpos = pos0.pop(len(pos0) - 1)
pos1.append(newpos)
ans.append(newpos)
m = max(newpos, m)
print(m)
ans_str = ""
for i in range(0, n):
if i + 1 == n:
ans_str += str(ans[i])
else:
ans_str += str(ans[i]) + " "
print(ans_str)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | f = lambda x: int(not x)
for _ in range(int(input())):
n = int(input())
l = list(map(int, input()))
s = {(0): [], (1): []}
a = []
c = 0
for v in l:
if s[f(v)]:
x = s[f(v)].pop()
s[v].append(x)
else:
c += 1
s[v].append(c)
x = c
a.append(x)
print(c)
print(*a) | ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER LIST LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for _ in range(t):
n = int(input())
li = input()
zl = []
ol = []
ans = [0] * len(li)
c = 0
muls = 1
for i in li:
if i == "0":
if len(ol) > 0:
ans[c] = ol.pop()
zl.append(ans[c])
else:
ans[c] = muls
zl.append(muls)
muls = muls + 1
elif len(zl) > 0:
ans[c] = zl.pop()
ol.append(ans[c])
else:
ans[c] = muls
ol.append(muls)
muls = muls + 1
c += 1
print(max(ans))
for i in ans:
print(i, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | n = int(input())
for _ in range(n):
nn = int(input())
ls = [[], []]
s = input()
last = 0
ans = [(0) for x in range(nn)]
for i, c in enumerate(s):
c = int(c)
if len(ls[c ^ 1]) == 0:
last += 1
ls[c].append(last)
ans[i] = last
else:
ans[i] = ls[c ^ 1][-1]
del ls[c ^ 1][-1]
ls[c].append(ans[i])
print(last)
print(" ".join(map(str, ans))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | from sys import stdin, stdout
input = stdin.readline
print = stdout.write
for _ in range(int(input())):
n = int(input())
s = input().strip()
ans = [1]
curr = 1
ones = set([1] if s[0] == "1" else [])
zeros = set([1] if s[0] == "0" else [])
for i in range(1, n):
if s[i] == "1":
if zeros:
for j in zeros:
break
zeros.remove(j)
ones.add(j)
ans.append(j)
else:
curr += 1
ones.add(curr)
ans.append(curr)
elif ones:
for j in ones:
break
ones.remove(j)
zeros.add(j)
ans.append(j)
else:
curr += 1
zeros.add(curr)
ans.append(curr)
print(str(max(ans)) + "\n")
ans = " ".join(list(map(str, ans)))
print(ans + "\n") | ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING LIST NUMBER LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING LIST NUMBER LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | a = int(input())
for i in range(a):
s = input()
s = input()
ans = [1]
last = [s[0]]
one = []
zero = []
if s[0] == "1":
zero.append(1)
else:
one.append(1)
cnt = 1
for i in range(1, len(s)):
if s[i] == "1":
if len(one) == 0 and len(zero) == 0:
ans.append(cnt)
zero.append(cnt)
elif len(one) != 0:
m = one.pop()
ans.append(m)
zero.append(m)
else:
cnt += 1
ans.append(cnt)
zero.append(cnt)
elif len(one) == 0 and len(zero) == 0:
ans.append(cnt)
one.append(cnt)
elif len(zero) != 0:
m = zero.pop()
ans.append(m)
one.append(m)
else:
cnt += 1
ans.append(cnt)
one.append(cnt)
print(cnt)
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for _ in range(t):
n = int(input())
s = input()
res = []
pos0 = []
pos1 = []
newpos = 1
for ch in s:
if ch == "0":
if pos1:
snum = pos1.pop()
curpos = snum
pos0.append(snum)
else:
curpos = newpos
pos0.append(newpos)
newpos += 1
elif pos0:
snum = pos0.pop()
curpos = snum
pos1.append(snum)
else:
curpos = newpos
pos1.append(newpos)
newpos += 1
res.append(curpos)
print(newpos - 1)
for p in res:
print(p, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def solve(s, n):
one, zero = [], []
if s[0] == "1":
one.append(1)
else:
zero.append(1)
counter = 1
ind = 1
ans = [0] * n
ans[0] = 1
while ind < n:
if s[ind] == "0":
if len(one):
ans[ind] = one.pop()
zero.append(ans[ind])
else:
counter += 1
zero.append(counter)
ans[ind] = counter
elif len(zero):
ans[ind] = zero.pop()
one.append(ans[ind])
else:
counter += 1
ans[ind] = counter
one.append(counter)
ind += 1
print(counter)
for val in ans:
print(val, end=" ")
print("\n", end="")
t = int(input())
for _ in range(t):
n = int(input())
s = input()
solve(s, len(s)) | FUNC_DEF ASSIGN VAR VAR LIST LIST IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | mod = 1000000007
t = int(input())
while t:
t -= 1
n = int(input())
s = input()
cnt = 1
sz = []
so = []
arr = [0] * n
for i in range(n):
if s[i] == "0":
if not so:
arr[i] = cnt
sz.append(cnt)
cnt += 1
else:
dd = so.pop()
sz.append(dd)
arr[i] = dd
elif not sz:
arr[i] = cnt
so.append(cnt)
cnt += 1
else:
dd = sz.pop()
so.append(dd)
arr[i] = dd
print(cnt - 1)
print(*arr) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for _ in range(t):
n = int(input())
s = map(int, input())
zeros = []
ones = []
count = 0
ans = []
for d in s:
if d == 0:
if zeros:
ans.append(zeros[-1])
ones.append(zeros.pop())
else:
count += 1
ans.append(count)
ones.append(count)
elif ones:
ans.append(ones[-1])
zeros.append(ones.pop())
else:
count += 1
ans.append(count)
zeros.append(count)
print(count)
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
while t > 0:
t -= 1
n = int(input())
s = input()
last0 = 0
last1 = 0
arr0 = [(0) for i in range(n + 1)]
arr1 = [(0) for i in range(n + 1)]
arr = [(0) for i in range(n)]
cur = 0
res = 0
for c in s:
if c == "0":
if last1 > 0:
last0 += 1
arr0[last0] = arr1[last1]
last1 -= 1
else:
last0 += 1
arr0[last0] = last0
arr[cur] = arr0[last0]
cur += 1
if arr0[last0] > res:
res = arr0[last0]
else:
if last0 > 0:
last1 += 1
arr1[last1] = arr0[last0]
last0 -= 1
else:
last1 += 1
arr1[last1] = last1
arr[cur] = arr1[last1]
cur += 1
if arr1[last1] > res:
res = arr1[last1]
print(res)
for i in range(n):
print(arr[i], end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | test = int(input())
for _ in range(test):
n = int(input())
arr = list(input())
seq = {(0): [], (1): []}
if arr[0] == "0":
seq[0].append([0])
else:
seq[1].append([0])
for i in range(1, n):
if arr[i] == "0":
if len(seq[1]):
con = seq[1].pop()
con.append(i)
seq[0].append(con)
else:
seq[0].append([i])
elif len(seq[0]):
con = seq[0].pop()
con.append(i)
seq[1].append(con)
else:
seq[1].append([i])
total = len(seq[0]) + len(seq[1])
res = [-1] * n
sub = 0
for i in range(len(seq[0])):
sub += 1
for j in seq[0][i]:
res[j] = sub
for i in range(len(seq[1])):
sub += 1
for j in seq[1][i]:
res[j] = sub
print(total)
for i in res:
print(i, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER LIST LIST IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER LIST NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER LIST VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER LIST VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for _ in range(t):
n = int(input())
st = input()
dic = {}
ct = []
one = []
zero = []
ch = 0
for i in range(len(st)):
c = st[i]
if c == "1":
if len(zero) > 0:
k = zero[-1]
ct.append(k)
one.append(k)
zero.pop()
dic[k] = 1
else:
ch += 1
one.append(ch)
ct.append(ch)
dic[ch] = 1
elif c == "0":
if len(one) > 0:
k = one[-1]
ct.append(k)
zero.append(k)
one.pop()
dic[k] = 1
else:
ch += 1
zero.append(ch)
ct.append(ch)
dic[ch] = 0
print(max(ct))
print(*ct) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
a = list(map(int, input()))
b = []
m = 0
e0 = []
e1 = []
for i in a:
if i == 0:
if e1 != []:
x = e1.pop()
e0.append(x)
b.append(x)
else:
m += 1
e0.append(m)
b.append(m)
elif e0 != []:
x = e0.pop()
e1.append(x)
b.append(x)
else:
m += 1
e1.append(m)
b.append(m)
print(m)
print(*b) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER IF VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
s = input()
dicti = {(0): [], (1): []}
ans = [-1] * n
k = 0
for z in range(n):
e = int(s[z])
if dicti[e]:
ai = dicti[e].pop()
dicti[1 - e].append(ai)
else:
k += 1
dicti[1 - e].append(k)
ai = k
ans[z] = ai
print(k)
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER LIST LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def op(val):
return "1" if val == "0" else "0"
t = int(input())
while t > 0:
n = int(input())
bin_str = list(input())
req = {}
ans = 0
loc = [(0) for i in range(n)]
for i in range(n):
s = bin_str[i]
found = False
if s in req and len(req[s]) > 0:
loc[i] = str(req[s].pop())
o = op(s)
if o in req:
req[o].append(loc[i])
else:
req[o] = [loc[i]]
else:
ans += 1
o = op(s)
loc[i] = str(ans)
if o in req:
req[o].append(ans)
else:
req[o] = [ans]
print(ans)
print(" ".join(loc))
t -= 1 | FUNC_DEF RETURN VAR STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [([c] * b) for i in range(a)]
def list3d(a, b, c, d):
return [[([d] * c) for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
INF = 10**19
MOD = 10**9 + 7
class BIT:
def __init__(self, n):
n += 1
nv = 1
while nv < n:
nv *= 2
self.size = nv
self.tree = [0] * nv
def sum(self, i):
s = 0
i += 1
while i > 0:
s += self.tree[i - 1]
i -= i & -i
return s
def add(self, i, x):
i += 1
while i <= self.size:
self.tree[i - 1] += x
i += i & -i
def get(self, l, r=None):
if r is None:
r = l + 1
res = 0
if r:
res += self.sum(r - 1)
if l:
res -= self.sum(l - 1)
return res
def update(self, i, x):
self.add(i, x - self.get(i))
def print(self, N):
for i in range(N):
print(self.get(i), end=" ")
print()
def bisearch_fore(self, l, r, x):
l_sm = self.sum(l - 1)
ok = r + 1
ng = l - 1
while ng + 1 < ok:
mid = (ok + ng) // 2
if self.sum(mid) - l_sm >= x:
ok = mid
else:
ng = mid
if ok != r + 1:
return ok
else:
return INF
for _ in range(INT()):
N = INT()
S = input()
i0 = i1 = 0
ans = [0] * N
last0 = BIT(N + 1)
last1 = BIT(N + 1)
for i in range(N + 1):
last0.add(i, 1)
last1.add(i, 1)
for i in range(N):
if S[i] == "0":
ans[i] = i0 + 1
i1 = min(i1, i0)
last0.update(i0, 0)
last1.update(i0, 1)
i0 = last0.bisearch_fore(i0, N, 1)
else:
ans[i] = i1 + 1
i0 = min(i0, i1)
last0.update(i1, 1)
last1.update(i1, 0)
i1 = last1.bisearch_fore(i1, N, 1)
print(max(ans))
print(*ans) | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF NONE IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
while t:
n = int(input())
a = list(map(lambda x: int(x), input()))
dic = {(0): [], (1): []}
for i in range(n):
dic[a[i]].append(i)
i, j = 0, 0
req = [(-1) for i in range(n)]
dic1 = {}
while j < len(dic[1]) and i < len(dic[0]):
if dic[0][i] < dic[1][j]:
req[dic[0][i]] = dic[1][j]
dic1[dic[1][j]] = True
i += 1
j += 1
i, j = 0, 0
while i < len(dic[0]) and j < len(dic[1]):
if dic[1][j] < dic[0][i]:
req[dic[1][j]] = dic[0][i]
dic1[dic[0][i]] = True
j += 1
i += 1
sub = 1
ans = [(-1) for i in range(n)]
dic2 = {}
for i in range(n):
if i not in dic2.keys():
if req[i] == -1 and i not in dic1.keys():
ans[i] = sub
sub += 1
else:
dic2[i] = True
j = i
while req[j] != -1:
ans[j] = sub
j = req[j]
dic2[j] = True
ans[j] = sub
dic2[j] = True
sub += 1
print(sub - 1)
for i in ans:
print(i, end=" ")
print()
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
c = 0
one = []
zero = []
n = int(input())
string = input()
output = []
for i in string:
if i == "0":
if len(one) == 0:
c += 1
zero.append(c)
output.append(c)
else:
x = one.pop()
zero.append(x)
output.append(x)
if i == "1":
if len(zero) == 0:
c += 1
one.append(c)
output.append(c)
else:
x = zero.pop()
one.append(x)
output.append(x)
print(c)
for i in output:
print(i, end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | from sys import gettrace, stdin
if gettrace():
inputi = input
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def solve():
n = int(input())
ss = input()
res = []
zs = []
os = []
nxt = 1
for s in ss:
if s == "0":
if os:
v = os.pop()
else:
v = nxt
nxt += 1
zs.append(v)
res.append(v)
else:
if zs:
v = zs.pop()
else:
v = nxt
nxt += 1
os.append(v)
res.append(v)
print(nxt - 1)
print(" ".join(map(str, res)))
def main():
t = int(input())
for _ in range(t):
solve()
main() | IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for _ in range(t):
n = int(input())
s = str(input())
val = {}
zero, one, mx = [], [], 0
for i in range(n):
if s[i] == "0":
if len(one) == 0:
mx += 1
val[mx] = [i]
zero.append(mx)
else:
seq = one.pop()
val[seq].append(i)
zero.append(seq)
elif len(zero) == 0:
mx += 1
val[mx] = [i]
one.append(mx)
else:
seq = zero.pop()
val[seq].append(i)
one.append(seq)
res = [_ for i in range(n)]
for i in val.keys():
for j in val[i]:
res[j] = i
print(mx)
for i in res:
print(i, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR LIST LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
a = input()
d = {(0): [], (1): []}
d[int(a[0])].append(0)
curr = 1
ans = [-1] * n
ans[0] = 0
for i in range(1, n):
flag = 0
if a[i] == "1":
if not d[0]:
d[1].append(curr)
ans[i] = curr
curr += 1
else:
k = d[0].pop()
d[1].append(k)
ans[i] = k
elif a[i] == "0":
if not d[1]:
d[0].append(curr)
ans[i] = curr
curr += 1
else:
k = d[1].pop()
d[0].append(k)
ans[i] = k
print(curr)
for i in ans:
print(i + 1, end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER LIST LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | T = int(input())
for i in range(T):
length = int(input())
s = input()
zero, one = [], []
arr = [0] * length
for i, char in enumerate(s):
if char == "0":
if not one:
new_pos = len(zero) + 1
zero.append(new_pos)
else:
new_pos = one.pop()
zero.append(new_pos)
elif not zero:
new_pos = len(one) + 1
one.append(new_pos)
else:
new_pos = zero.pop()
one.append(new_pos)
arr[i] = new_pos
print(len(one) + len(zero))
print(" ".join(str(item) for item in arr)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
input = sys.stdin.readline
T = int(input())
class SegmentTree:
def __init__(self, n, ele):
self.ide_ele = ele
self.num = 2 ** (n - 1).bit_length()
self.seg = [self.ide_ele] * 2 * self.num
def segfunc(self, x, y):
return min(x, y)
def init(self, init_val):
for i in range(n):
self.seg[i + self.num - 1] = init_val[i]
for i in range(self.num - 2, -1, -1):
self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2])
def update(self, k, x):
k += self.num - 1
self.seg[k] = x
while k:
k = (k - 1) // 2
self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])
def query(self, p, q):
if q <= p:
return self.ide_ele
p += self.num - 1
q += self.num - 2
res = self.ide_ele
while q - p > 1:
if p & 1 == 0:
res = self.segfunc(res, self.seg[p])
if q & 1 == 1:
res = self.segfunc(res, self.seg[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = self.segfunc(res, self.seg[p])
else:
res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
return res
for testcase in range(T):
n = int(input())
s = list(input())
one = SegmentTree(n, 10**10)
zero = SegmentTree(n, 10**10)
a = [0] * n
for i in range(n):
if s[i] == "0":
zero.update(i, i)
else:
one.update(i, i)
k = 0
for i in range(n):
if a[i] != 0:
continue
k += 1
a[i] = k
idx = i
if s[i] == "0":
flag = True
else:
flag = False
while True:
if flag:
p = one.query(idx, n)
if p == 10**10:
break
idx = p
flag = False
a[idx] = k
one.update(idx, 10**10)
else:
p = zero.query(idx, n)
if p == 10**10:
break
idx = p
flag = True
a[idx] = k
zero.update(idx, 10**10)
print(k)
print(*a) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def func(a, s):
current = ""
currIndex = 1
prev0 = 0
prev1 = 0
prev0Index = []
prev1Index = []
res = []
currmax = 1
for i in range(a):
if i == 0:
currIndex = 1
res = [1]
if s[i] == "0":
prev0 += 1
prev0Index.append(currIndex)
elif s[i] == "1":
prev1 += 1
prev1Index.append(currIndex)
else:
if s[i] == "0":
if s[i - 1] == "1":
if len(prev1Index) != 0:
if res[-1] == prev1Index[-1]:
prev1 -= 1
prev1Index.pop()
temp = res[-1]
res.append(temp)
prev0 += 1
prev0Index.append(temp)
elif prev1 != 0:
prev1 -= 1
temp = prev1Index.pop()
res.append(temp)
prev0 += 1
prev0Index.append(temp)
else:
prev0 += 1
temp = currmax + 1
res.append(temp)
prev0Index.append(temp)
elif s[i] == "1":
if s[i - 1] == "0":
if len(prev0Index) != 0:
if res[-1] == prev0Index[-1]:
prev0 -= 1
prev0Index.pop()
temp = res[-1]
res.append(temp)
prev1 += 1
prev1Index.append(temp)
elif prev0 != 0:
prev0 -= 1
temp = prev0Index.pop()
res.append(temp)
prev1 += 1
prev1Index.append(temp)
else:
prev1 += 1
temp = currmax + 1
res.append(temp)
prev1Index.append(temp)
if res[-1] > currmax:
currmax = res[-1]
print(currmax)
print(*res)
t = int(input())
for _ in range(t):
a = int(input())
s = input()
func(a, s) | FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for nt in range(int(input())):
n = int(input())
s = input()
ans = []
o, z = 0, 0
d = {}
l = {(0): {}, (1): {}}
for i in range(n):
if s[i] == "0":
d[i] = o + 1
if o == len(ans):
ans.append(["0"])
l[0][o] = 1
o += 1
else:
ans[o].append("0")
z = o
l[0][o] = 1
if o in l[1]:
del l[1][o]
o = len(ans)
for k in l[1]:
o = k
break
else:
d[i] = z + 1
if z == len(ans):
ans.append(["1"])
l[1][z] = 1
z += 1
else:
ans[z].append("1")
o = z
l[1][z] = 1
if z in l[0]:
del l[0][z]
z = len(ans)
for k in l[0]:
z = k
break
print(len(ans))
for i in range(n):
print(d[i], end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR DICT NUMBER NUMBER DICT DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST STRING ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | from sys import stdin
inp = lambda: stdin.readline().strip()
t = int(inp())
for _ in range(t):
n = int(inp())
s = list(inp())
zeroes = []
ones = []
ans = []
for i in range(len(s)):
pos = len(zeroes) + len(ones)
if s[i] == "0":
if not ones:
zeroes.append(pos)
else:
pos = ones.pop()
zeroes.append(pos)
elif not zeroes:
ones.append(pos)
else:
pos = zeroes.pop()
ones.append(pos)
ans.append(pos + 1)
print(len(zeroes) + len(ones))
print(*ans) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
R = lambda: map(int, input().split())
answersNumbers = []
answersBelonging = []
for i in range(0, t):
n = int(input())
a = input()
maxNumber = 0
onePossiblesSub = []
zeroPossibleSub = []
numbers = []
for c in a:
if c == "1":
if not onePossiblesSub:
maxNumber = maxNumber + 1
onePossiblesSub.append(maxNumber)
popped = onePossiblesSub.pop()
numbers.append(popped)
zeroPossibleSub.append(popped)
if c == "0":
if not zeroPossibleSub:
maxNumber = maxNumber + 1
zeroPossibleSub.append(maxNumber)
popped = zeroPossibleSub.pop()
onePossiblesSub.append(popped)
numbers.append(popped)
answersBelonging.append(numbers)
answersNumbers.append(maxNumber)
for i in range(0, t):
print(answersNumbers[i])
for j in range(0, len(answersBelonging[i])):
print(answersBelonging[i][j], sep="", end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING IF VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
s = input()
sss0 = []
sss1 = []
if s[0] == "0":
sss0.append(1)
else:
sss1.append(1)
result = [1]
result_counter = 1
i = 1
while i < n:
if s[i] == "0":
if len(sss1) == 0:
result_counter += 1
sss0.append(result_counter)
result.append(result_counter)
else:
nex = sss1.pop()
result.append(nex)
sss0.append(nex)
elif len(sss0) == 0:
result_counter += 1
sss1.append(result_counter)
result.append(result_counter)
else:
nex = sss0.pop()
result.append(nex)
sss1.append(nex)
i += 1
print(result_counter)
print(" ".join(map(str, result))) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | from sys import stdin
inp = lambda: stdin.readline().strip()
t = int(inp())
for _ in range(t):
n = int(inp())
s = inp()
zeroes = []
ones = []
groups = [0] * n
g = 2
flag = False
if s[0] == "0":
ones.append(1)
else:
zeroes.append(1)
groups[0] = 1
for i in range(1, n):
if s[i] == "0":
if zeroes:
groups[i] = zeroes[len(zeroes) - 1]
ones.append(zeroes.pop())
else:
flag = True
elif ones:
groups[i] = ones[len(ones) - 1]
zeroes.append(ones.pop())
else:
flag = True
if flag:
groups[i] = g
if s[i] == "0":
ones.append(g)
else:
zeroes.append(g)
g += 1
flag = False
print(g - 1)
print(*groups) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
input = lambda: sys.stdin.readline().rstrip()
for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input()]
st = [[], []]
ans = []
cur = 0
for i in a:
if i == 1:
if not st[0]:
st[i].append(cur + 1)
else:
st[i].append(st[0].pop())
cur = max(cur, st[i][-1])
else:
if not st[1]:
st[i].append(cur + 1)
else:
st[i].append(st[1].pop())
cur = max(cur, st[i][-1])
ans.append(st[i][-1])
print(max(ans))
print(*ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for ip in [*open(0)][2::2]:
a = list(map(int, ip.strip()))
n = len(a)
p0, p1, ans = [], [], []
for x in a:
np = len(p0) + len(p1)
if x == 0:
if p1:
np = p1.pop()
p0.append(np)
else:
p0.append(np)
elif p0:
np = p0.pop()
p1.append(np)
else:
p1.append(np)
ans.append(np + 1)
print(len(p0) + len(p1))
print(*ans) | FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def parse_array(array_str, n):
arr = [int] * n
for i in range(0, n):
num = int(array_str[i])
arr[i] = num
return arr
def count_subsequences(arr, n):
sequence = [0] * n
ones_queue = []
zeros_queue = []
sequence_number = 1
for i in range(n):
if arr[i] == 0:
if len(zeros_queue) != 0:
sequence[i] = zeros_queue.pop(0)
ones_queue.append(sequence[i])
else:
sequence[i] = sequence_number
ones_queue.append(sequence[i])
sequence_number = sequence_number + 1
elif arr[i] == 1:
if len(ones_queue) != 0:
sequence[i] = ones_queue.pop(0)
zeros_queue.append(sequence[i])
else:
sequence[i] = sequence_number
zeros_queue.append(sequence[i])
sequence_number = sequence_number + 1
return sequence_number - 1, sequence
test_cases = int(input(""))
for a in range(0, test_cases):
n = int(input(""))
binary_str = input("")
binary_arr = parse_array(binary_str, n)
subsequence_count, sequence = count_subsequences(binary_arr, n)
print(subsequence_count)
print(*sequence) | FUNC_DEF ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
_ = input()
s = [(1 if ch == "1" else 0) for ch in input()]
balance = 0
labels = [0] * len(s)
stacks = [[], []]
max_label = 0
for i, ch in enumerate(s):
if stacks[1 - ch]:
j = stacks[1 - ch].pop()
stacks[ch].append(i)
labels[i] = labels[j]
else:
labels[i] = max_label + 1
max_label += 1
stacks[ch].append(i)
print(max(labels))
print(*labels) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | cases = int(input())
for t in range(cases):
n = int(input())
s = input()
d = {"0": [], "1": []}
v = []
c = 1
for i in s:
if i == "0":
if d["1"]:
val = d["1"].pop()
d["0"].append(val)
else:
d["0"].append(c)
c += 1
elif d["0"]:
val = d["0"].pop()
d["1"].append(val)
else:
d["1"].append(c)
c += 1
v.append(d[i][-1])
print(max(v))
print(*v) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING LIST LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING VAR VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def check(A, seq):
dict_a = dict()
for i in range(len(seq)):
if seq[i] not in dict_a.keys():
dict_a[seq[i]] = A[i]
elif A[i] == dict_a[seq[i]]:
print(A, seq, dict_a[seq[i]])
raise EOFError
else:
dict_a[seq[i]] = A[i]
T = int(input())
for t in range(T):
N = input()
A = input()
state = A[0]
unresolved = 0
unresolved_state = A[0]
sub_string = []
sub_count = 0
for a in A:
if a == unresolved_state:
unresolved += 1
sub_string.append(unresolved)
elif unresolved >= 1:
sub_string.append(unresolved)
unresolved -= 1
else:
unresolved = max(sub_string) + 1
sub_string.append(unresolved)
unresolved_state = a
check(A, sub_string)
print(max(sub_string))
print(" ".join(list(map(str, sub_string)))) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())
for _ in range(t):
n = int(input())
string = [int(x) for x in input()]
needsOne = set()
needsZero = set()
buckets = {}
c = 0
for ind, val in enumerate(string):
if val == 1:
if len(needsOne):
cand = needsOne.pop()
buckets[cand].append(ind)
needsZero.add(cand)
else:
c += 1
buckets[c] = [ind]
needsZero.add(c)
elif len(needsZero):
cand = needsZero.pop()
buckets[cand].append(ind)
needsOne.add(cand)
else:
c += 1
buckets[c] = [ind]
needsOne.add(c)
ans = ["0" for x in range(n)]
for k in buckets:
for val in buckets[k]:
ans[val] = str(k)
print(len(buckets))
print(" ".join(ans)) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
x = int(input())
mylist = input()
sz = []
so = []
count = 1
out = []
for i in range(x):
if mylist[i] == "0":
if so:
sz.append(so.pop())
else:
sz.append(count)
count += 1
out.append(sz[-1])
else:
if sz:
so.append(sz.pop())
else:
so.append(count)
count += 1
out.append(so[-1])
print(count - 1)
print(*out) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for t in range(int(input())):
n = int(input())
s = input()
d = [0] * n
k = 0
p0 = []
p1 = []
for i in range(n):
if s[i] == "0":
if len(p1) == 0:
k += 1
p = k
else:
p = p1.pop()
p0.append(p)
else:
if len(p0) == 0:
k += 1
p = k
else:
p = p0.pop()
p1.append(p)
d[i] = p
print(k)
print(*d) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write
def input():
return fast_reader().strip()
def print(*argv):
fast_writer(" ".join(str(i) for i in argv))
fast_writer("\n")
for _ in range(int(input())):
n = int(input())
s = input()
l = [int(i) for i in s]
pp = [[], []]
ans = [(0) for i in range(n)]
for i in range(n):
temp = len(pp[0]) + len(pp[1])
if l[i] == 0:
if len(pp[1]) == 0:
pp[0].append(temp)
else:
temp = pp[1].pop()
pp[0].append(temp)
elif len(pp[0]) == 0:
pp[1].append(temp)
else:
temp = pp[0].pop()
pp[1].append(temp)
ans[i] = temp + 1
print(len(pp[0]) + len(pp[1]))
print(*ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
pos0, pos1 = [], []
res_num = 0
res_ls = []
for bit in input():
if bit == "1":
if not pos0:
res_num += 1
num = res_num
else:
num = pos0.pop()
res_ls.append(num)
pos1.append(num)
else:
if not pos1:
res_num += 1
num = res_num
else:
num = pos1.pop()
res_ls.append(num)
pos0.append(num)
print(res_num)
for num in res_ls:
print(num, end=" ") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR STRING IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
s = input()
p0 = []
p1 = []
seq = [0] * n
last = 0
for i in range(n):
if s[i] == "0":
if p1:
q = p1.pop()
p0.append(q)
else:
last += 1
q = last
p0.append(q)
elif s[i] == "1":
if p0:
q = p0.pop()
p1.append(q)
else:
last += 1
q = last
p1.append(q)
seq[i] = q
print(len(p0) + len(p1))
print(*seq) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | from sys import maxsize, stdin, stdout
input = stdin.readline
def solve():
pass
test = 1
test = int(input().strip())
for t in range(0, test):
n = int(input().strip())
s = list(input().strip())
b = [-1] * n
one = []
zero = []
cnt = 1
for i in range(n):
if s[i] == "0":
if one == []:
zero += [cnt]
b[i] = cnt
cnt += 1
else:
temp = one.pop()
zero += [temp]
b[i] = temp
elif zero == []:
one += [cnt]
b[i] = cnt
cnt += 1
else:
temp = zero.pop()
one += [temp]
b[i] = temp
print(cnt - 1)
def onedarr(a):
for el in a:
print(el, end=" ")
print()
onedarr(b)
ans = solve() | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR LIST VAR LIST VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR VAR IF VAR LIST VAR LIST VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
st = input()
end0 = []
end1 = []
cnt = 1
arr = []
for idx, s in enumerate(st):
if s == "0":
if not end1:
end0.append(cnt)
arr.append(cnt)
cnt += 1
else:
num = end1.pop()
end0.append(num)
arr.append(num)
elif not end0:
end1.append(cnt)
arr.append(cnt)
cnt += 1
else:
num = end0.pop()
end1.append(num)
arr.append(num)
print(len(end0) + len(end1))
print(*arr) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
def solve():
n = int(input())
s = input()
ones = []
zeros = []
assign = [None] * n
seq = 0
for i, x in enumerate(s):
if x == "1":
if len(zeros):
loc = zeros.pop()
assign[i] = loc
ones.append(loc)
else:
seq += 1
assign[i] = seq
ones.append(seq)
elif len(ones):
loc = ones.pop()
assign[i] = loc
zeros.append(loc)
else:
seq += 1
zeros.append(seq)
assign[i] = seq
print(seq)
print(*assign, sep=" ")
return
while t:
solve()
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING RETURN WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | from sys import stdin, stdout
def ufind(u_a, i):
if i == u_a[i]:
return i
u_a[i] = ufind(u_a, u_a[i])
return u_a[i]
def union(u_a, i, j):
ri = ufind(u_a, i)
rj = ufind(u_a, j)
u_a[ri] = u_a[rj]
def binary_string_to_subsequences2(n, s):
u_a = [i for i in range(n)]
q_a = [[] for _ in range(2)]
q_a[ord(s[0]) - ord("0")].append(0)
for i in range(1, n):
v = ord(s[i]) - ord("0")
if q_a[v ^ 1]:
union(u_a, i, q_a[v ^ 1].pop())
q_a[v].append(i)
ans1 = len(q_a[0]) + len(q_a[1])
ans2 = []
for i in range(n):
ans2.append(ufind(u_a, i) + 1)
return [ans1, ans2]
def binary_string_to_subsequences(n, s):
g = 1
q_a = [[] for _ in range(2)]
q_a[ord(s[0]) - ord("0")].append(g)
ans2 = [g]
for i in range(1, n):
v = ord(s[i]) - ord("0")
if q_a[v ^ 1]:
q_a[v].append(q_a[v ^ 1].pop())
else:
g += 1
q_a[v].append(g)
ans2.append(q_a[v][-1])
ans1 = len(q_a[0]) + len(q_a[1])
return [ans1, ans2]
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
s = stdin.readline().strip()
ans = binary_string_to_subsequences(n, s)
stdout.write(str(ans[0]) + "\n")
stdout.write(" ".join(map(str, ans[1])) + "\n") | FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN LIST VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER STRING |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def arri():
return list(map(int, input().split()))
t = int(input())
for _ in range(t):
n = int(input())
arr = list(input())
haveZero, haveOne = [], []
ans = [0] * n
val = 0
for i in range(n):
if arr[i] == "1" and haveZero:
ans[i] = haveZero.pop()
haveOne.append(ans[i])
elif arr[i] == "1":
val += 1
ans[i] = val
haveOne.append(val)
if arr[i] == "0" and haveOne:
ans[i] = haveOne.pop()
haveZero.append(ans[i])
elif arr[i] == "0":
val += 1
ans[i] = val
haveZero.append(val)
print(max(ans))
print(" ".join(map(str, ans))) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
s = str(input())
lz = []
lo = []
ans = 0
l = []
for i in range(n):
if s[i] == "0":
if len(lo) != 0:
x = lo[-1]
lo.pop()
lz.append(x)
l.append(x)
else:
ans += 1
lz.append(ans)
l.append(ans)
elif len(lz) != 0:
x = lz[-1]
lz.pop()
lo.append(x)
l.append(x)
else:
ans += 1
lo.append(ans)
l.append(ans)
print(ans)
for i in range(n):
print(l[i], end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | rw = int(input())
for qwe in range(rw):
n = int(input())
s = input()
a = []
tF = []
tT = []
tn = 0
tt = 0
for i in range(n):
if s[i] == "0" and tn == tt:
a.append(tn)
tn += 1
tT.append(tn)
tt += 1
elif s[i] == "0":
f = tF[0]
a.append(f - 1)
tF.pop(0)
tT.append(f)
tt += 1
elif s[i] == "1" and tt == 0:
a.append(tn)
tn += 1
tF.append(tn)
elif s[i] == "1":
f = tT[0]
a.append(f - 1)
tT.pop(0)
tF.append(f)
tt -= 1
print(tn)
for i in a:
print(i + 1, end=" ")
print("") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
def main():
res = ""
input = sys.stdin.readline
print = sys.stdout.write
t = int(input())
for _ in range(t):
n = int(input())
s = input()
zeros = []
k_zeros = 0
ones = []
k_ones = 0
nums = []
k = 0
for i in range(n):
if s[i] == "1":
k_ones += 1
if k_zeros != 0:
k_zeros -= 1
nums.append(str(zeros[-1]))
ones.append(zeros[-1])
del zeros[-1]
else:
k += 1
ones.append(k)
nums.append(str(k))
else:
k_zeros += 1
if k_ones != 0:
k_ones -= 1
nums.append(str(ones[-1]))
zeros.append(ones[-1])
del ones[-1]
else:
k += 1
zeros.append(k)
nums.append(str(k))
res += str(k) + "\n" + " ".join(nums) + "\n"
print(res)
main() | IMPORT FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for i in range(t):
n = int(input())
s = input()
l = []
l1 = []
l2 = []
ans = 0
for j in range(n):
if s[j] == "0":
if len(l1):
a = l1.pop()
l.append(a)
l2.append(a)
else:
ans += 1
l2.append(ans)
l.append(ans)
elif len(l2):
a = l2.pop()
l.append(a)
l1.append(a)
else:
ans += 1
l1.append(ans)
l.append(ans)
print(ans)
print(*l) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | T = int(input())
for _ in range(T):
n = int(input())
a = input()
count = 1
zer = []
one = []
res = [(0) for i in range(n)]
for i in range(n):
if a[i] == "0":
if one == []:
zer.append(count)
count += 1
res[i] = zer[-1]
else:
x = one.pop()
zer.append(x)
res[i] = zer[-1]
if a[i] == "1":
if zer == []:
one.append(count)
count += 1
res[i] = one[-1]
else:
x = zer.pop()
one.append(x)
res[i] = one[-1]
print(count - 1)
print(*res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING IF VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
s = list(map(int, list(input())))
a = s[:]
addone = []
addzero = []
nextsub = 1
for i, c in enumerate(s):
if c == 1:
if len(addone) > 0:
sub = addone.pop(0)
a[i] = sub
addzero.append(sub)
else:
addzero.append(nextsub)
a[i] = nextsub
nextsub += 1
elif len(addzero) > 0:
sub = addzero.pop(0)
a[i] = sub
addone.append(sub)
else:
addone.append(nextsub)
a[i] = nextsub
nextsub += 1
print(nextsub - 1)
print(*a) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def solution():
n = int(input())
s = list(input())
zero = []
one = []
ans = 0
grp = 0
grparr = []
for i in range(n):
if s[i] == "0":
if one != []:
zero.append(one[-1])
one.pop()
grparr.append(zero[-1])
else:
grp += 1
ans += 1
zero.append(grp)
grparr.append(grp)
elif zero != []:
one.append(zero[-1])
grparr.append(one[-1])
zero.pop()
else:
ans += 1
grp += 1
one.append(grp)
grparr.append(grp)
print(ans)
print(*grparr)
t = int(input())
for _ in range(t):
solution() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for t in range(int(input())):
n = int(input())
arr = [int(i) for i in input()[:n]]
ans, temp, exp = [], [[], []], 0
for i in arr:
x = 1 - i
if len(temp[x]) > 0:
p = temp[x].pop()
temp[i].append(p)
else:
exp += 1
temp[i].append(exp)
p = exp
ans.append(p)
print(max(ans))
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST LIST LIST LIST NUMBER FOR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | T = int(input())
for _ in range(T):
N = int(input())
s = input()
A = [(-1) for i in range(N)]
zol, onl = [], []
zo, on = 0, 0
cs = 0
for i in range(N):
if s[i] == "0":
if on > 0:
on = on - 1
A[i] = onl.pop()
else:
cs = cs + 1
A[i] = cs
zol.append(A[i])
zo = zo + 1
else:
if zo > 0:
zo = zo - 1
A[i] = zol.pop()
else:
cs = cs + 1
A[i] = cs
onl.append(A[i])
on = on + 1
print(cs)
print(*A) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
T = int(input())
for _ in range(T):
N = int(input())
S = list(sys.stdin.readline()[:-1])
A = []
cnt = 0
ans = []
zero = []
one = []
for comp in S:
if not A:
A.append([comp])
ans.append(1)
if comp == "0":
zero.append(0)
else:
one.append(0)
elif comp == "0":
if one:
zero.append(one.pop())
ans.append(zero[-1] + 1)
else:
cnt += 1
zero.append(cnt)
ans.append(cnt + 1)
elif comp == "1":
if zero:
one.append(zero.pop())
ans.append(one[-1] + 1)
else:
cnt += 1
one.append(cnt)
ans.append(cnt + 1)
print(cnt + 1)
print(" ".join(map(str, ans))) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
s = input()
subCount = 0
i = 0
a0 = []
a1 = []
ans = []
while i < n:
if subCount == 0:
run = True
while i < n and run:
if i + 1 < n and s[i] == s[i + 1]:
if s[i] == "0":
a0.append(0)
else:
a1.append(0)
run = False
ans.append(1)
i += 1
subCount += 1
else:
found = False
idx = 0
if s[i] == "0":
if len(a1):
idx = a1.pop()
found = True
elif len(a0):
idx = a0.pop()
found = True
if not found:
ln = subCount
run = True
while i < n and run:
if i + 1 < n and s[i] == s[i + 1]:
if s[i] == "0":
a0.append(ln)
else:
a1.append(ln)
run = False
ans.append(ln + 1)
i += 1
subCount += 1
else:
ln = subCount
run = True
while i < n and run:
if i + 1 < n and s[i] == s[i + 1]:
if s[i] == "0":
a0.append(idx)
else:
a1.append(idx)
run = False
ans.append(idx + 1)
i += 1
print(subCount)
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for _ in range(t):
n = int(input())
s = input()
ans = [0] * n
c = 0
q = [[], []]
for i in range(n):
x = int(s[i]) ^ 1
if len(q[x]) == 0:
c = c + 1
ans[i] = c
else:
ans[i] = q[x][-1]
q[x].pop()
q[x ^ 1].append(ans[i])
print(max(ans))
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
INP = lambda: sys.stdin.readline().strip()
INT = lambda: int(INP())
MAP = lambda: map(int, INP().split())
ARR = lambda: [int(i) for i in INP().split()]
def JOIN(arr, x=" "):
return x.join([str(i) for i in arr])
def EXIT(x="NO"):
print(x)
exit()
for _ in range(INT()):
n = INT()
arr = [int(i) for i in list(INP())]
stk = [-1] * n
stk[0] = arr[0]
z, o = [1], []
if arr[0] == 1:
z, o = o, z
ans = [1] * n
c = 1
for i in range(1, n):
if arr[i] == 0:
if len(o):
x = o.pop()
z.append(x)
ans[i] = x
else:
c += 1
z.append(c)
ans[i] = c
elif len(z):
x = z.pop()
o.append(x)
ans[i] = x
else:
c += 1
o.append(c)
ans[i] = c
print(c)
print(JOIN(ans)) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST NUMBER LIST IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for s in [*open(0)][2::2]:
a, k, r = [[], []], 0, []
for v in map(int, s[:-1]):
if not a[v]:
k += 1
a[v] += [k]
r += [a[v][-1]]
a[1 - v] += [a[v].pop()]
print(k, *r) | FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR LIST LIST LIST NUMBER LIST FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR LIST VAR VAR LIST VAR VAR NUMBER VAR BIN_OP NUMBER VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for _ in range(t):
end0, end1 = [], []
n = int(input())
s = str(input())
count = 0
ans = [(0) for _ in range(n)]
for j in range(len(s)):
if s[j] == "0":
if len(end1) > 0:
index = end1.pop()
ans[j] = index
end0.append(index)
else:
count += 1
end0.append(count)
ans[j] = count
elif len(end0) > 0:
index = end0.pop()
ans[j] = index
end1.append(index)
else:
count += 1
end1.append(count)
ans[j] = count
print(count)
for i in ans:
print(i, end=" ")
print("") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
t = input()
a = [1] * n
z = []
o = []
g = 1
for i in range(n):
if t[i] == "0":
if o:
x = o.pop()
a[i] = x
z.append(x)
else:
a[i] = g
z.append(g)
g += 1
elif z:
x = z.pop()
a[i] = x
o.append(x)
else:
a[i] = g
o.append(g)
g += 1
print(max(a))
print(*a) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def d(s, n):
ans = [0] * n
stack0 = []
stack1 = []
partition = len(stack0) + len(stack1)
for i in range(n):
partition = len(stack0) + len(stack1)
if s[i] == "0":
if len(stack1) == 0:
stack0.append(partition)
else:
partition = stack1[-1]
stack1.pop()
stack0.append(partition)
elif len(stack0) == 0:
stack1.append(partition)
else:
partition = stack0[-1]
stack0.pop()
stack1.append(partition)
ans[i] = partition
return ans
t = int(input())
for _ in range(t):
n = int(input())
s = input()
res = d(s, n)
for i in range(len(res)):
res[i] = res[i] + 1
print(max(res))
print(*res) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for T in range(t):
n = int(input())
s = input()
current_surplus = s[0]
stacks = [1]
stack = 1
maxS = 1
for i in s[1:]:
if i == current_surplus:
stack += 1
maxS = max(maxS, stack)
stacks.append(stack)
else:
stacks.append(max(stack, 1))
stack = max(stack - 1, 0)
if stack == 0:
if current_surplus == "0":
current_surplus = "1"
else:
current_surplus = "0"
stack = maxS
print(max(stacks))
print(" ".join([str(i) for i in stacks])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for _ in range(t):
n = int(input())
s = input()
num = 0
zeros = 0
ones = 0
d0 = set()
d1 = set()
a = []
for x in s:
if x == "1":
if zeros > 0:
temp = d0.pop()
d1.add(temp)
a.append(temp)
zeros -= 1
else:
num += 1
d1.add(num)
a.append(num)
ones += 1
else:
if ones > 0:
temp = d1.pop()
d0.add(temp)
a.append(temp)
ones -= 1
else:
num += 1
d0.add(num)
a.append(num)
zeros += 1
print(num)
print(" ".join(map(str, a))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for i in range(t):
n = int(input())
a = list(input())
b = []
count = 0
d = {"0": [], "1": []}
for i in a:
if i == "1":
arr = d["0"]
if not len(arr) == 0:
tmp = arr.pop()
b.append(tmp)
d["1"].append(tmp)
else:
count += 1
b.append(count)
d["1"].append(count)
else:
arr = d["1"]
if not len(arr) == 0:
tmp = arr.pop()
b.append(tmp)
d["0"].append(tmp)
else:
count += 1
b.append(count)
d["0"].append(count)
print(count)
print(*b) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING LIST LIST FOR VAR VAR IF VAR STRING ASSIGN VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
input = sys.stdin.readline
T = int(input())
def update_min2(k, x):
k += num_min - 1
seg_min2[k] = x
while k:
k = (k - 1) // 2
seg_min2[k] = min(seg_min2[k * 2 + 1], seg_min2[k * 2 + 2])
def query_min2(p, q):
if q <= p:
return ide_ele_min
p += num_min - 1
q += num_min - 2
res = ide_ele_min
while q - p > 1:
if p & 1 == 0:
res = min(res, seg_min2[p])
if q & 1 == 1:
res = min(res, seg_min2[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = min(res, seg_min2[p])
else:
res = min(min(res, seg_min2[p]), seg_min2[q])
return res
def update_min(k, x):
k += num_min - 1
seg_min[k] = x
while k:
k = (k - 1) // 2
seg_min[k] = min(seg_min[k * 2 + 1], seg_min[k * 2 + 2])
def query_min(p, q):
if q <= p:
return ide_ele_min
p += num_min - 1
q += num_min - 2
res = ide_ele_min
while q - p > 1:
if p & 1 == 0:
res = min(res, seg_min[p])
if q & 1 == 1:
res = min(res, seg_min[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = min(res, seg_min[p])
else:
res = min(min(res, seg_min[p]), seg_min[q])
return res
for testcase in range(T):
n = int(input())
s = list(input())
ide_ele_min = 10**10
num_min = 2 ** (n - 1).bit_length()
seg_min = [ide_ele_min] * 2 * num_min
seg_min2 = [ide_ele_min] * 2 * num_min
a = [0] * n
for i in range(n):
if s[i] == "0":
update_min(i, i)
else:
update_min2(i, i)
k = 0
for i in range(n):
if a[i] != 0:
continue
k += 1
a[i] = k
idx = i
if s[i] == "0":
flag = True
else:
flag = False
while True:
if flag:
p = query_min2(idx, n)
if p == 10**10:
break
idx = p
flag = False
a[idx] = k
update_min2(idx, 10**10)
else:
p = query_min(idx, n)
if p == 10**10:
break
idx = p
flag = True
a[idx] = k
update_min(idx, 10**10)
print(k)
print(*a) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def solve():
n = int(input())
s = input()
total = 0
ans = [(0) for _ in range(n)]
min_k = 0
max_k = 0
for ss in s:
if ss == "1":
total += 1
max_k = max(max_k, total)
else:
total -= 1
min_k = min(min_k, total)
k = max_k - min_k
print(k)
plus = abs(min_k)
total = 0
for i, ss in enumerate(s):
if ss == "1":
total += 1
ans[i] = total + plus
else:
ans[i] = total + plus
total -= 1
print(*ans)
t = int(input())
for _ in range(t):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
s = list(str(input()))
a = [1]
one = []
zero = []
if s[0] == "0":
zero.append(1)
else:
one.append(1)
ind = 1
if n == 1:
print(1)
print(1)
else:
for i in range(1, n):
if s[i] == "0":
if len(one) == 0:
ind += 1
zero.append(ind)
a.append(ind)
else:
a.append(one[0])
zero.append(one[0])
one.pop(0)
elif len(zero) == 0:
ind += 1
one.append(ind)
a.append(ind)
else:
a.append(zero[0])
one.append(zero[0])
zero.pop(0)
print(max(a), *a) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def solve(n, a):
s = a
stack = []
stack2 = []
ans = [0] * n
npiles = 0
index = 0
for i in range(n):
if s[i] == "0":
if len(stack2) == 0:
npiles += 1
ans[index] = npiles
stack.append(npiles)
else:
p = stack2.pop(0)
ans[index] = p
stack.append(p)
elif len(stack) == 0:
npiles += 1
ans[index] = npiles
stack2.append(npiles)
else:
p = stack.pop(0)
ans[index] = p
stack2.append(p)
index += 1
print(npiles)
return ans
t = int(input())
while t:
n = int(input())
a = input()
print(*solve(n, a))
t -= 1 | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def calc(s, n):
k = 0
r = []
zeros = []
ones = []
for a in s:
if a == "0":
src = ones
dst = zeros
else:
src = zeros
dst = ones
if src:
x = src.pop()
else:
k += 1
x = str(k)
r.append(x)
dst.append(x)
return k, " ".join(r)
T = int(input())
for t in range(T):
n = int(input())
s = input()
assert len(s) == n
k, r = calc(s, n)
print(k)
print(r) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
def solve(S):
bits = [(0 if c == "0" else 1) for c in S]
DEBUG = False
if DEBUG:
print(S)
n = len(S)
assigned = [-1] * n
STACK_Z = []
STACK_O = []
first = bits[0]
cnt = 1
if first == 0:
STACK_Z.append(cnt)
else:
STACK_O.append(cnt)
assigned[0] = cnt
i = 1
for bit in bits[1:]:
if bit == 0:
same = STACK_Z
other = STACK_O
else:
same = STACK_O
other = STACK_Z
if len(other) == 0:
cnt += 1
same.append(cnt)
assigned[i] = cnt
else:
val = other.pop(-1)
same.append(val)
assigned[i] = val
i += 1
print(cnt)
print(" ".join([str(x) for x in assigned]))
return cnt
IN = [x.strip() for x in sys.stdin.readlines()]
T = int(IN[0])
cur = 1
for ti in range(T):
S = IN[cur + 1]
solve(S)
cur += 2 | IMPORT FUNC_DEF ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for i in range(t):
n = int(input())
s = input().strip()
an = [1]
z = []
o = []
c = 1
if s[0] == "1":
z.append(1)
else:
o.append(1)
for j in range(1, n):
if s[j] == "0":
if z != []:
p = z[-1]
o.append(p)
an.append(p)
del z[-1]
else:
c = c + 1
o.append(c)
an.append(c)
elif o != []:
p = o[-1]
z.append(p)
an.append(p)
del o[-1]
else:
c = c + 1
an.append(c)
z.append(c)
print(c)
print(" ".join(map(str, an))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR LIST ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for gg in range(t):
ans = 1
a = [1]
k1 = []
k0 = []
n = int(input())
s = input()
lst = s[0]
if s[0] == "1":
k1.append(1)
else:
k0.append(1)
for i in range(1, n):
if s[i] == "1":
if len(k0) != 0:
cur = k0.pop()
k1.append(cur)
a.append(cur)
else:
ans += 1
k1.append(ans)
a.append(ans)
lst = "1"
else:
if len(k1) != 0:
cur = k1.pop()
k0.append(cur)
a.append(cur)
else:
ans += 1
k0.append(ans)
a.append(ans)
lst = "0"
print(ans)
print(*a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
s = [(True if x == "1" else False) for x in input()]
sub = [[], []]
ans = [None for i in range(n)]
m = 1
for i in range(n):
e = not s[i]
if len(sub[e]) != 0:
f = sub[e].pop()
ans[i] = f
sub[s[i]].append(f)
else:
ans[i] = m
sub[s[i]].append(m)
m += 1
print(m - 1)
for i in range(len(ans) - 1):
print(ans[i], end=" ")
print(ans[-1]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | ones = [(0) for i in range(2 * 10**5 + 1)]
zeros = [(0) for i in range(2 * 10**5 + 1)]
for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input()]
d = [(0) for i in range(n)]
o = -1
z = -1
c = 1
for i in range(n):
if a[i] == 0:
if o == -1:
z += 1
zeros[z] = c
d[i] = c
c += 1
else:
k = ones[o]
d[i] = k
o -= 1
z += 1
zeros[z] = k
elif z == -1:
o += 1
ones[o] = c
d[i] = c
c += 1
else:
k = zeros[z]
d[i] = k
z -= 1
o += 1
ones[o] = k
print(c - 1)
print(*d) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
T = int(sys.stdin.readline())
for _ in range(T):
n = int(sys.stdin.readline())
s = list(map(int, list(sys.stdin.readline().strip())))
list_res = [0] * n
list_check = [[], []]
list_check[s[0]].append(1)
list_res[0] = 1
now = 1
for i in range(1, n):
key = not s[i]
if list_check[key]:
list_check[s[i]].append(list_check[key].pop())
list_res[i] = list_check[s[i]][-1]
else:
now += 1
list_check[s[i]].append(now)
list_res[i] = now
print(now)
print(*list_res) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST LIST LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def solve(n, s):
pos0 = []
pos1 = []
answ = [0] * n
for i in range(n):
newpos = len(pos0) + len(pos1)
if s[i] == "0":
if len(pos1) == 0:
pos0.append(newpos)
else:
newpos = pos1.pop()
pos0.append(newpos)
elif len(pos0) == 0:
pos1.append(newpos)
else:
newpos = pos0.pop()
pos1.append(newpos)
answ[i] = newpos
print(len(pos0) + len(pos1))
for i in answ:
print(i + 1, "", end="")
print()
t = int(input())
for i in range(t):
n = int(input())
s = input()
solve(n, s) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def sol(a):
b = []
p = [[], []]
c = 0
for i in range(0, len(a)):
if a[i] == "1":
if len(p[0]):
b[p[0][-1]].append(i)
p[1].append(p[0][-1])
p[0].pop()
else:
b.append([])
b[-1].append(i)
p[1].append(len(b) - 1)
elif len(p[1]):
b[p[1][-1]].append(i)
p[0].append(p[1][-1])
p[1].pop()
else:
b.append([])
b[-1].append(i)
p[0].append(len(b) - 1)
return b
test = int(input())
for t in range(0, test):
n = int(input())
a = input()
b = sol(a)
c = []
for i in range(0, len(a)):
c.append(0)
for i in range(0, len(b)):
for e in b[i]:
c[e] = i + 1
print(max(c))
for e in c:
print(e, end=" ")
print()
def decToBi(i, p):
a = []
for t in range(0, p):
a.append(i % 2)
i = i // 2
a.reverse()
return a
def check(c):
for i in range(1, len(c)):
if c[i - 1] == c[i]:
return 0
return 1
def main():
p = 6
for i in range(0, 2**p):
a = decToBi(i, p)
b = sol(a)
c = []
for i in range(0, p):
c.append(0)
for i in range(0, len(b)):
for e in b[i]:
c[e] = i + 1
for i in range(0, max(c)):
d = []
for j in range(0, p):
if c[j] == i + 1:
d.append(a[j])
if check(d) == 0:
print(a)
print(b)
print(i + 1, d)
print() | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | from sys import stdin
input = lambda: stdin.readline().rstrip("\r\n")
for _ in range(int(input())):
n = int(input())
ans = [1] * n
s = input()
needzero = set()
needone = set()
if s[0] == "0":
needone.add(1)
else:
needzero.add(1)
cur = 2
for i in range(1, n):
if s[i] == "0":
if len(needzero) == 0:
ans[i] = cur
needone.add(cur)
cur += 1
else:
f = needzero.pop()
ans[i] = f
needone.add(f)
elif len(needone) == 0:
ans[i] = cur
needzero.add(cur)
cur += 1
else:
f = needone.pop()
ans[i] = f
needzero.add(f)
print(cur - 1)
print(*ans) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | from sys import stdin, stdout
fullans = ""
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
s = stdin.readline()
ar = [-1] * n
ar[0] = 1
ans = ""
ans += str(ar[0]) + " "
cnt = 1
zerostack = []
onestack = []
if int(s[0]):
onestack.append(1)
else:
zerostack.append(1)
for i in range(1, n):
if int(s[i]):
if len(zerostack) == 0:
cnt += 1
onestack.append(cnt)
ar[i] = cnt
ans += str(ar[i]) + " "
else:
elem = zerostack.pop()
onestack.append(elem)
ar[i] = elem
ans += str(ar[i]) + " "
elif len(onestack) == 0:
cnt += 1
zerostack.append(cnt)
ar[i] = cnt
ans += str(ar[i]) + " "
else:
elem = onestack.pop()
zerostack.append(elem)
ar[i] = elem
ans += str(ar[i]) + " "
fullans += str(len(set(ar))) + "\n"
fullans += ans + "\n"
stdout.write(fullans) | ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | t = int(input())
for _ in range(t):
n = int(input())
s = input()
pos_0 = []
pos_1 = []
a = [0] * n
count = 1
for i in range(n):
if s[i] == "0":
if pos_1 == []:
pos_0.append(count)
a[i] = count
count += 1
else:
x = pos_1.pop()
a[i] = x
pos_0.append(x)
elif pos_0 == []:
pos_1.append(count)
a[i] = count
count += 1
else:
x = pos_0.pop()
a[i] = x
pos_1.append(x)
print(count - 1)
print(*a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def solve(n, s):
sol = 0
taken = [-1] * n
zeros = []
ones = []
for i in range(n):
if s[i] == "1":
if not zeros:
sol += 1
x = zeros.pop() if zeros else sol
ones.append(x)
else:
if not ones:
sol += 1
x = ones.pop() if ones else sol
zeros.append(x)
taken[i] = x
return sol, taken
t = int(input())
for _ in range(t):
n = int(input())
s = input()
sol, taken = solve(n, s)
print(sol)
print(" ".join(map(str, taken))) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | tests = int(input())
for t in range(tests):
n = int(input())
arr = list(map(int, input()))
arr_res = [None for _ in range(n)]
curr = 1
check = True
curr_item = None
prev_start_idx = -1
one_group = []
zero_group = []
for idx, item in enumerate(arr):
if item == 0:
if len(one_group) == 0:
zero_group.append(curr)
arr_res[idx] = curr
curr += 1
else:
one_member = one_group.pop(-1)
arr_res[idx] = one_member
zero_group.append(one_member)
elif item == 1:
if len(zero_group) == 0:
one_group.append(curr)
arr_res[idx] = curr
curr += 1
else:
zero_member = zero_group.pop(-1)
arr_res[idx] = zero_member
one_group.append(zero_member)
print(curr - 1)
print(" ".join(list(map(str, arr_res)))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
s = input().rstrip()
s = [s[-i - 1] for i in range(n)]
ans = []
zero = []
one = []
res = [-1] * n
pos = 0
while s:
b = s.pop()
if b == "0":
if not one:
new = 1
ans.append(new)
res[pos] = len(ans)
zero.append(len(ans) - 1)
else:
id = one.pop()
ans[id] += 1
res[pos] = id + 1
zero.append(id)
elif not zero:
new = 1
ans.append(new)
res[pos] = len(ans)
one.append(len(ans) - 1)
else:
id = zero.pop()
ans[id] += 1
res[pos] = id + 1
one.append(id)
pos += 1
print(len(ans))
print(*res) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | for _ in range(int(input())):
n = int(input())
a = input()
cans = 1
ans = [1]
now = 1
lasts = [2, int(a[0])]
if int(a[0]) == 0:
zero = [1]
one = []
else:
zero = []
one = [1]
for i in range(n - 1):
check = int(a[i + 1])
if check == 1:
if zero:
lol = zero.pop()
ans.append(lol)
one.append(lol)
else:
now += 1
ans.append(now)
cans += 1
one.append(now)
elif one:
lol = one.pop()
ans.append(lol)
zero.append(lol)
else:
now += 1
ans.append(now)
cans += 1
zero.append(now)
print(cans)
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def fun(s):
count_0 = []
count_1 = []
ans = [None for i in range(len(s))]
for i in range(len(s)):
curr = s[i]
if curr == "1":
if count_0:
ans[i] = str(count_0.pop(-1))
count_1.append(int(ans[i]))
else:
ans[i] = str(len(count_0) + len(count_1) + 1)
count_1.append(int(ans[i]))
elif count_1:
ans[i] = str(count_1.pop(-1))
count_0.append(int(ans[i]))
else:
ans[i] = str(len(count_0) + len(count_1) + 1)
count_0.append(int(ans[i]))
return len(count_0) + len(count_1), ans
for _ in range(int(input())):
n = input()
s = input()
a, b = fun(s)
print(a)
print(" ".join(b)) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING IF VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def minSubs(s):
zero = []
one = []
pos = [1]
d = {"0": 0, "1": 0}
d[s[0]] += 1
if s[0] == "0":
zero = [1]
else:
one = [1]
for i in range(1, len(s)):
if s[i] == "0":
if d["1"] > 0:
d["1"] -= 1
d["0"] += 1
pos.append(one[-1])
zero.append(one[-1])
one.remove(one[-1])
else:
d["0"] += 1
pos.append(max(zero[0], zero[-1]) + 1)
zero.append(pos[-1])
elif d["0"] > 0:
d["0"] -= 1
d["1"] += 1
pos.append(zero[-1])
one.append(zero[-1])
zero.remove(zero[-1])
else:
d["1"] += 1
pos.append(max(one[0], one[-1]) + 1)
one.append(pos[-1])
return d["0"] + d["1"], pos
t = int(input())
for _ in range(t):
n = int(input())
s = input()
a1, a2 = minSubs(s)
print(a1)
print(" ".join(map(str, a2))) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR DICT STRING STRING NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR STRING NUMBER VAR STRING NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR STRING NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR STRING VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a binary string $s$ consisting of $n$ zeros and ones.
Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).
Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of $s$. The second line of the test case contains $n$ characters '0' and '1' β the string $s$.
It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).
-----Output-----
For each test case, print the answer: in the first line print one integer $k$ ($1 \le k \le n$) β the minimum number of subsequences you can divide the string $s$ to. In the second line print $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the number of subsequence the $i$-th character of $s$ belongs to.
If there are several answers, you can print any.
-----Example-----
Input
4
4
0011
6
111111
5
10101
8
01010000
Output
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4 | def read_nums():
return [int(x) for x in input().split()]
def solve():
_ = read_nums()
s = input()
zeros, ones = [], []
res = []
for ch in s:
if ch == "1":
if len(zeros) == 0:
ones.append(len(zeros) + len(ones) + 1)
res.append(len(zeros) + len(ones))
else:
index = zeros.pop()
res.append(index)
ones.append(index)
elif len(ones) == 0:
zeros.append(len(zeros) + len(ones) + 1)
res.append(len(zeros) + len(ones))
else:
index = ones.pop()
res.append(index)
zeros.append(index)
print(max(res))
print(" ".join(map(str, res)))
def main():
(t,) = read_nums()
for _ in range(t):
solve()
main() | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.