description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split(" ")) grid = [] for i in range(n): grid.append(input()) frontier = [(0, 0)] total = 0 while len(frontier) > 0: cur = frontier.pop(0) if cur[0] < n and cur[1] < m: if grid[cur[0]][cur[1]] == "*": total += 1 frontier = [] frontier.append((cur[0], cur[1] + 1)) frontier.append((cur[0] + 1, cur[1])) print(total)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
from sys import stderr, stdin input = stdin.readline def dbp(*args, **kwargs): pass def get_int_list(): return [int(x) for x in input().strip().split()] h, w = get_int_list() rows = [input().strip() for _ in range(h)] r = c = t = eaten = 0 while r < h and c < w: dbp(r, c, h, w, eaten) if rows[r][c] == "*": eaten += 1 std = rows[r].find("*", c + 1) dbp(rows[r], std) mvdown = False if std == -1: mvdown = True else: std -= c i = 1 while r + i < h: rstd = rows[r + i].find("*", c) if rstd == -1: i += 1 continue else: if i + rstd < std: mvdown = True break i += 1 if mvdown == True: dbp("trying to move down") if r < h - 1: r += 1 else: c += 1 dbp("result:", r, c) else: dbp("trying to move across") if c < w - 1: c += 1 else: r += 1 dbp("result:", r, c) print(eaten)
ASSIGN VAR VAR FUNC_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR STRING VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) field = [] for line in range(n): field.append(list(input())) x = 0 y = 0 berries = 0 while x != m - 1 or y != n - 1: if field[y][x] == "*": berries += 1 if x == m - 1: y += 1 elif y == n - 1: x += 1 elif field[y][x + 1] == "*": x += 1 elif field[y + 1][x] == "*": y += 1 else: x += 1 if field[y][x] == "*": berries += 1 print(berries)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
def canReach(x1, y1, x2, y2): return x1 <= x2 and y1 <= y2 def nearest(x, y): global cake, H, W for i in range(H * W): x2, y2 = repere[i] if canReach(x, y, x2, y2) and cake[y2][x2] == "*": return x2, y2 return None, None H, W = map(int, input().split()) cake = [] repere = {} x, y = 0, 0 i = 0 while i < H * W: repere[i] = x, y x -= 1 y += 1 if x < 0 or y >= H: x += y + 1 y = max(0, x - W + 1) x = min(x, W - 1) i += 1 for _ in range(H): cake += (list(input()),) ans = 0 x, y = 0, 0 while 1: try: x, y = nearest(x, y) cake[y][x] = "." ans += 1 except TypeError: break print(ans)
FUNC_DEF RETURN VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR STRING RETURN VAR VAR RETURN NONE NONE ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR STRING VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
s = [] n, m = map(int, input().split()) for i in range(n): s.append(input()) x, y = 0, 0 z = 0 if s[x][y] == "*": z += 1 while x < n - 1 or y < m - 1: if x == n - 1: y += 1 elif y == m - 1: x += 1 elif s[x][y + 1] == "*": y += 1 elif s[x + 1][y] == "*": x += 1 else: y += 1 if s[x][y] == "*": z += 1 print(z)
ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split()) a = [] for i in range(h): a.append(input()) i = 1 j = 1 res = 0 dir = True while i < h or j < w: if a[i - 1][j - 1] == "*": res += 1 if i == h: j += 1 elif j == w: i += 1 elif a[i][j - 1] == a[i - 1][j]: j += 1 elif a[i][j - 1] == "*": i += 1 else: j += 1 if a[h - 1][w - 1] == "*": res += 1 print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
H, W = map(int, input().split()) rows = [] for _ in range(H): rows.append(input()) ans = 0 x, y = 0, 0 while True: ans += rows[x][y] == "*" if (x, y) == (H - 1, W - 1): break nx, ny = None, None mindist = float("inf") for i in range(x, H): for j in range(y, W): if rows[i][j] == ".": continue dist = abs(i - x) + abs(j - y) if not dist: continue if dist < mindist: mindist = dist nx = i ny = j if mindist == float("inf"): break x = nx y = ny print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER VAR VAR VAR VAR STRING IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NONE NONE ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
class Test: def __init__(self): self.a = 1 def input(self): line = input() line = [int(i) for i in line.split(" ")] M, N = line[0], line[1] map = [] for i in range(M): line = input().strip() line = [i for i in line] map.append(line) x = 0 y = 0 ans = 0 if map[0][0] == "*": ans = 1 while True: flag = False for i in range(1, M * N): for j in range(i + 1): posx = min(M - 1, x + j) posy = min(N - 1, y + i - j) if map[posx][posy] == "*": flag = True ans += 1 x = posx y = posy map[posx][posy] = "." break if flag: break if flag != True: print(ans) break a = Test() a.input()
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR STRING IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split()) grid = [input() for i in range(h)] x = 0 y = 0 ans = 0 while x < h - 1 or y < w - 1: if grid[x][y] == "*": ans += 1 if x == h - 1: y += 1 elif y == w - 1: x += 1 else: d1 = 200 d2 = 200 for i in range(x, h): for j in range(y, w): if i == x and j == y: continue if grid[i][j] == ".": continue dd1 = abs(i - (x + 1)) + abs(j - y) dd2 = abs(i - x) + abs(j - (y + 1)) d1 = min(d1, dd1) d2 = min(d2, dd2) if d1 < d2: x += 1 else: y += 1 if grid[h - 1][w - 1] == "*": ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = [int(i) for i in input().strip().split()] l = [[i for i in input()] for i in range(h)] count = 0 i, j = 0, 0 if l[0][0] == "*": count += 1 while True: if i == h - 1 and j == w - 1: break down = -1 right = -1 if i + 1 < h: if l[i + 1][j] == "*": down = 1 else: down = 0 if j + 1 < w: if l[i][j + 1] == "*": right = 1 else: right = 0 if down == 1: i += 1 count += 1 continue if right == 1: j += 1 count += 1 continue if right == 0 and down == 0: j += 1 continue if right == -1: i += 1 continue if down == -1: j += 1 continue print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
def calc_dist(t1, t2): x1, y1 = t1 x2, y2 = t2 return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5 h, w = [int(x) for x in input().split()] arr = [["0"] * w] * h locations = [] for i in range(h): line = [x for x in input()] for j in range(w): if line[j] == "*": locations.append([i, j]) arr[i][j] = line[j] locations.append([h, w]) count = 0 curr = [0, 0] closest = [h, w] dist = 99999999999999999999999999999999999999999999 if curr in locations: count += 1 while curr != [h, w]: dist = 99999999999999999999999999999999999999999999 for location in locations: if location[0] < curr[0] or location[1] < curr[1] or location == curr: continue if calc_dist(curr, location) < dist: dist = calc_dist(curr, location) closest = location curr = closest if curr != [h, w]: count += 1 print(count)
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST STRING VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
H, W = map(int, input().split()) S = [input() for i in range(H)] x, y = 0, 0 ans = 0 if S[0][0] == "*": ans += 1 while True: nx, ny = None, None for i in range(H): for j in range(W): if S[i][j] == "*" and i >= x and j >= y and (i, j) != (x, y): if nx == None: nx, ny = i, j elif abs(nx - x) + abs(ny - y) > abs(i - x) + abs(j - y): nx, ny = i, j if nx != None: x, y = nx, ny ans += 1 else: break print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER WHILE NUMBER ASSIGN VAR VAR NONE NONE FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) q = [input() for i in range(n)] i = j = c = 0 while 1: c += q[i][j] == "*" if i == n - 1 and j == m - 1: break if i == n - 1: j += 1 elif j == m - 1: i += 1 elif q[i + 1][j] == "*": i += 1 elif q[i][j + 1] == "*": j += 1 else: j += 1 print(c)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE NUMBER VAR VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) cake = [list(input()) for i in range(n)] loc = 0, 0 ans = 1 if cake[0][0] == "*" else 0 while loc != (n, m): best = 1000 bestloc = n, m x, y = loc for i in range(x, n): for j in range(y, m): if cake[i][j] == "*" and (i, j) != (x, y): dis = i - x + j - y if dis < best: best = dis bestloc = i, j loc = bestloc if best < 1000: ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER STRING NUMBER NUMBER WHILE VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split()) board = [input() for _ in range(h)] out = 0 cx = 0 cy = 0 if board[0][0] == "*": out += 1 while True: poss = [] for i in range(h): for j in range(w): if board[i][j] == "*" and i >= cx and j >= cy and i + j > cx + cy: poss.append((i + j, i, j)) if poss: _, cx, cy = min(poss) out += 1 else: break print(out)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split()) a = [] for _ in range(h): a.append(input()) i, j, k = 0, 0, 0 while i < h: if a[i][j] == "*": k += 1 if i != h - 1 and a[i + 1][j] == "*" or j == w - 1: i += 1 else: j += 1 print(k)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
import sys h, w = map(int, input().split()) lines = [] for i in range(h): lines.append(input()) ci, cj = 0, 0 ans = 1 if lines[0][0] == "*" else 0 while True: ni, nj = 1000, 1000 for i in range(ci, h): for j in range(cj, w): if ( i + j > ci + cj and lines[i][j] == "*" and i - ci + (j - cj) < ni - ci + (nj - cj) ): ni, nj = i, j if ni == 1000: break ci, cj = ni, nj ans += 1 print(ans)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER STRING NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR STRING BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) grid = [input() for i in range(n)] ci, cj = 0, 0 ate = 0 if grid[0][0] == "*": ate += 1 while 1: berry = [] for i in range(ci, n): for j in range(cj, m): if ci == i and cj == j: continue if grid[i][j] == "*": berry.append((i, j)) berry.sort(key=lambda T: (T[0] + T[1], T[0])) if berry: ate += 1 ci, cj = berry[0] else: break print(ate)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
def fun(i, j): if i >= hw[0] or j >= hw[1]: return [10000, i, j] if a[i][j] == "*": return [0, i, j] else: right = fun(i + 1, j) down = fun(i, j + 1) if right[0] < down[0]: right[0] += 1 return right else: down[0] += 1 return down h, w = [int(i) for i in input().split()] hw = [h, w] a = [] for i in range(h): curr = input() a.append([i for i in curr]) ans = 0 i = 0 j = 0 while i < h and j < w: ans += 1 curr = fun(i, j) i, j = curr[1], curr[2] if i < h and j < w: a[i][j] = "." print(ans - 1)
FUNC_DEF IF VAR VAR NUMBER VAR VAR NUMBER RETURN LIST NUMBER VAR VAR IF VAR VAR VAR STRING RETURN LIST NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER RETURN VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) a = [] for i in range(n): p = input() new = [] for j in range(m): new.append(p[j]) a.append(new) ans = 0 x = 0 y = 0 while x != n - 1 or y != m - 1: if a[x][y] == "*": ans += 1 if y != m - 1 and ( x + 1 < n and a[x + 1][y] == "." or y + 1 < m and a[x][y + 1] == "*" ): y += 1 elif x + 1 == n: y += 1 else: x += 1 if a[x][y] == "*": print(ans + 1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR STRING BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) anime = [] for i in range(n): s = input() anime.append(s) x = 0 y = 0 ans = 0 if anime[0][0] == "*": ans += 1 while True: a = n b = m for i in range(x, n): for j in range(y, m): if (i != x or j != y) and anime[i][j] == "*" and a + b > i + j: a = i b = j if a == n: break ans += 1 x = a y = b print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR STRING BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split()) grid = [] for i in range(h): grid.append(input()) r, c = 0, 0 ans = 0 for i in range(h + w - 2): if grid[r][c] == "*": ans += 1 if r == h - 1: c += 1 continue if c == w - 1: r += 1 continue right = 111111, 111 for row in range(r, h): if "*" in grid[row][c + 1 :]: right = min(right, (grid[row][c + 1 :].index("*"), row - r)) down = 11111, 111 for col in range(c, w): column = [grid[r][col] for r in range(r + 1, h)] if "*" in column: down = min(down, (column.index("*"), col - c)) if down < right: r += 1 else: c += 1 if grid[r][c] == "*": ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF STRING VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF STRING VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split(" ")) s = [] for i in range(h): s.append(input()) i = 0 j = 0 c = 0 while i < h or j < w: if s[i][j] == "*": c = c + 1 if j < w - 1 and s[i][j + 1] == "*": j = j + 1 elif i < h - 1 and s[i + 1][j] == "*": i = i + 1 elif i == h - 1 and j < w - 1: j = j + 1 elif j == w - 1 and i < h - 1: i = i + 1 elif j < w - 1: j = j + 1 elif i < h - 1: i = i + 1 else: break print(c)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
import sys def getDist(i, j, ii, jj): return abs(ii - i) + abs(jj - j) def main(): n, m = readIntArr() grid = [] for _ in range(n): grid.append(input()) berries = [] for i in range(n): for j in range(m): if grid[i][j] == "*": berries.append((i, j)) ans = 0 i = 0 j = 0 if grid[i][j] == "*": ans += 1 while True: potential = [] for ii, jj in berries: if ii > i and jj >= j or ii >= i and jj > j: potential.append((ii, jj)) if len(potential) == 0: break minDist = inf for ii, jj in potential: minDist = min(minDist, getDist(i, j, ii, jj)) potential2 = [] for ii, jj in potential: if getDist(i, j, ii, jj) == minDist: potential2.append((ii, jj)) minI = inf for ii, jj in potential2: minI = min(minI, ii) for ii, jj in potential2: if ii == minI: i = ii j = jj break ans += 1 print(ans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _aa in range(1): main()
IMPORT FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = [int(i) for i in input().split()] a = [None] * n for i in range(n): a[i] = [(1 if i == "*" else 0) for i in input()] cc = 0, 0 res = 0 curdir = 1 while True: if a[cc[0]][cc[1]]: res += 1 if cc == (n - 1, m - 1): break distright = 1000 if cc[0] == n - 1: distright = 10000 for j in range(cc[0] + 1, n): if a[j][cc[1]]: distright = j - cc[0] break distdown = 1000 if cc[1] == m - 1: distdown = 10000 for j in range(cc[1] + 1, m): if a[cc[0]][j]: distdown = j - cc[1] break if distright < distdown: nxt = cc[0] + 1, cc[1] else: nxt = cc[0], cc[1] + 1 cc = nxt print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split(" ")) lis = [] for _ in range(h): lis.append(input()) i, j = 0, 0 count = 0 if lis[h - 1][w - 1] == "*": count += 1 while i != h - 1 or j != w - 1: if lis[i][j] == "*": count += 1 if j == w - 1: i = i + 1 elif i == h - 1: j = j + 1 try: if lis[i][j + 1] == "*": j = j + 1 elif lis[i + 1][j] == "*": i = i + 1 elif lis[i][j + 1] == ".": j = j + 1 elif lis[i + 1][j] == ".": i = i + 1 except: pass print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) a = [[x for x in input()] for _ in range(n)] i = 0 j = 0 ans = 0 while True: found = False for x in range(100): for t in range(0, x + 1): ni = i + t nj = j + x - t if ni < n and nj < m and a[ni][nj] == "*": ans += 1 found = True i = ni j = nj a[ni][nj] = "." break if found: break if not found: break print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR STRING IF VAR IF VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
H, W = map(int, input().split()) cake = [([False] * W) for i in range(H)] for i in range(H): s = input() for j in range(W): if s[j] == "*": cake[i][j] = True x, y = 0, 0 ans = 0 while x < H and y < W: if cake[x][y]: ans += 1 if y < W - 1 and cake[x][y + 1]: y += 1 continue if x < H - 1 and cake[x + 1][y]: x += 1 continue if y < W - 1: y += 1 continue elif x < H - 1: x += 1 continue if x == H - 1 and y == W - 1: break print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
a, b = map(int, input().split()) matrix = [] for _ in range(a): l = input() matrix.append(l) x = 0 y = 0 c = 0 while x < a - 1 or y < b - 1: if matrix[x][y] == "*": c += 1 if x == a - 1: y += 1 continue elif y == b - 1: x += 1 continue xB = False yB = False if x < a - 1 and matrix[x + 1][y] == "*": xB = True if y < b - 1 and matrix[x][y + 1] == "*": yB = True if xB == yB: y += 1 elif xB: x += 1 else: y += 1 if matrix[x][y] == "*": c += 1 print(c)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = list(map(int, input().split())) arr = [] points = [] for i in range(h): arr.append(input()) for j in range(len(arr[i])): if arr[i][j] == "*": points.append([i, j]) start = [0, 0] end = [h - 1, w - 1] cur = [0, 0] count = 0 while cur != end: min_dist = 100 next = [] for point in points: if point[0] >= cur[0] and point[1] >= cur[1]: dist = point[0] - cur[0] + (point[1] - cur[1]) if dist < min_dist: min_dist = dist next = point if min_dist == 100: cur = end break else: cur = next del points[points.index(next)] count += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = map(int, input().split()) arr = [input() for i in range(h)] a = 0 b = 0 cnt = int(arr[a][b] == "*") while True: x = 10**9 y = 10**9 for i in range(a, h): for j in range(b, w): if i == a and j == b: continue if arr[i][j] == ".": continue if i - a + j - b < x - a + y - b: x = i y = j if x == 10**9: break a = x b = y cnt += 1 print(cnt)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR STRING WHILE NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR STRING IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) a = [input() for i in range(n)] l = [] for i in range(n): for j in range(m): if a[i][j] == "*": l += [(i, j)] now = 0, 0 ans = int(now in l) while True: mn = 99 mnt = now for i in l: if i != now and i[0] >= now[0] and i[1] >= now[1] and mn > sum(i) - sum(now): mn = sum(i) - sum(now) mnt = i if mn == 99: break now = mnt ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR LIST VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) a = [input() for i in range(n)] count = 0 i = 0 j = 0 for k in range(n + m - 1): if a[i][j] == "*": count += 1 look_down = -1 look_right = -1 for q in range(i + 1, n): if a[q][j] == "*": look_down = q for q in range(j + 1, m): if a[i][q] == "*": look_right = q if look_down == -1 and look_right == -1 and j != m - 1: j += 1 elif look_down == -1 and j != m - 1: j += 1 elif look_right == -1 and i != n - 1: i += 1 elif look_down - i >= look_right - j and j != m - 1: j += 1 else: i += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
r, c = map(int, input().split()) cake = [] for i in range(r): cake.append(list(input())) i = 0 j = 0 berries = 0 if cake[0][0] == "*": berries += 1 while i < r and j < c: if j + 1 < c and i < r: if cake[i][j + 1] == "*": berries += 1 j += 1 elif i + 1 < r and j < c: if cake[i + 1][j] == "*": berries += 1 i += 1 else: j += 1 else: j += 1 elif i + 1 < r and j < c: if cake[i + 1][j] == "*": berries += 1 i += 1 else: j += 1 else: i += 1 if j < c - 1: for k in range(j + 1, c): if cake[i - 1][k] == "*": berries += 1 if i < r - 1: for k in range(i + 1, r): if cake[k][j - 1] == "*": berries += 1 print(berries)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
from itertools import dropwhile, filterfalse, islice, takewhile def valida(coord): x, y = coord return not (x < cols and y < rens and matriz[y][x]) rens, cols = map(int, input().split()) matriz = [list(map(lambda x: x == "*", input())) for i in range(rens)] bayas = dist = x = y = 0 while x + y + dist < cols + rens: listdist = zip(range(x + dist, x - 1, -1), range(y, y + 1 + dist)) listdist = filterfalse(valida, listdist) listdist = list(islice(listdist, 1)) if listdist != []: bayas += 1 coord = listdist[0] x, y = coord dist = 1 else: dist += 1 print(bayas)
FUNC_DEF ASSIGN VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
y, x = list(map(int, input().split())) board = [] for i in range(y): board.append(input()) posX = 0 posY = 0 score = 0 turn = 0 last = "y" while True: if posY >= y or posX >= x: break if board[posY][posX] == "*": score += 1 if posX + 1 < x and board[posY][posX + 1] == "*": posX += 1 last = "x" elif posY + 1 < y and board[posY + 1][posX] == "*": posY += 1 last = "y" elif last == "x": if posY + 1 < y: posY += 1 last = "y" else: posX += 1 last = "x" elif posX + 1 < x: posX += 1 last = "x" else: posY += 1 last = "y" print(score)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR STRING IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR STRING IF VAR STRING IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING VAR NUMBER ASSIGN VAR STRING IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
def try1(n, m): down = n right = m i = 0 j = 0 ans = 0 iter = 0 while i < n and j < m: if matrix[i][j] == "*": ans += 1 if i == n - 1: if j == m - 1: break else: j += 1 elif iter % 2 == 0 or j == m - 1: i += 1 else: j += 1 iter += 1 return ans def try2(n, m): down = n right = m i = 0 j = 0 ans = 0 iter = 0 while i < n and j < m: if matrix[i][j] == "*": ans += 1 if i == n - 1: if j == m - 1: break else: j += 1 elif iter % 2 == 1 or j == m - 1: i += 1 else: j += 1 iter += 1 return ans def try3(n, m): down = n right = m i = 0 j = 0 ans = 0 iter = 0 while i < n and j < m: if matrix[i][j] == "*": ans += 1 if i == n - 1 and j == m - 1: break r = i + 1 sr = 0 fr = False while r < n: sr += 1 if matrix[r][j] == "*": fr = True break r += 1 c = j + 1 sc = 0 fc = False while c < m: sc += 1 if matrix[i][c] == "*": fc = True break c += 1 if fc and fr: if sc <= sr: j = c else: i = r elif fc: j = c elif fr: i = r elif j < m - 1: j += 1 else: i += 1 return ans def try4(n, m): li = [] for i in range(n): for j in range(m): if matrix[i][j] == "*": li.append((i, j)) nearest_best = -1 r = 0 c = 0 ans = 0 while True: nearest_best = -1 dist_best = 100 for i in range(len(li)): x, y = li[i] if x >= r and y >= c: curr_dist = x - r + (y - c) if curr_dist < dist_best: dist_best = curr_dist nearest_best = i elif curr_dist == dist_best: if x < li[nearest_best][0]: nearest_best = i if nearest_best == -1: break ans += 1 r = li[nearest_best][0] c = li[nearest_best][1] li.pop(nearest_best) return ans n, m = map(int, input().split()) matrix = [] for i in range(n): row = input() matrix.append(row) ans = try4(n, m) print(ans)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
a, b = map(int, input().split()) cake = [] for _ in range(a): cake.append([x for x in input()]) count = 0 x, y = 0, 0 while x < b and y < a: if cake[y][x] == "*": count += 1 if x < b - 1 and cake[y][x + 1] == "*": x += 1 elif y < a - 1 and cake[y + 1][x] == "*": y += 1 else: x += 1 if x == b: x -= 1 y += 1 print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
n, m = map(int, input().split()) g = [input() for i in range(n)] def rec(i, j, ans): if i == n - 1 and j == m - 1: print(ans + (g[i][j] == "*")) exit() add = g[i][j] == "*" if j == m - 1: return rec(i + 1, j, ans + add) if i == n - 1: return rec(i, j + 1, ans + add) if g[i][j + 1] == "*": return rec(i, j + 1, ans + add) if g[i + 1][j] == "*": return rec(i + 1, j, ans + add) return rec(i, j + 1, ans + add) rec(0, 0, 0)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER STRING RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR STRING RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = [int(x) for x in input().split()] maps = [] for i in range(h): row = input() rows = [] for r in row: rows.append(r) maps.append(rows) ans = 0 if maps[0][0] == "." else 1 i, j = 0, 0 while not (i == h - 1 and j == w - 1): if j + 1 < w and maps[i][j + 1] == "*": j += 1 ans += 1 elif i + 1 < h and maps[i + 1][j] == "*": i += 1 ans += 1 elif j + 1 < w: j += 1 else: i += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER STRING NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
h, w = list(map(int, input().split())) t = [input() for _ in range(h)] collected = 0 curr_pos = 0, 0 if t[0][0] == "*": collected = 1 def collectable(i, j): return i >= curr_pos[0] and j >= curr_pos[1] def dist(i, j): return i - curr_pos[0] + (j - curr_pos[1]) while True: closest = None for i in range(curr_pos[0], h): for j in range(curr_pos[1], w): if t[i][j] == "*" and not (i, j) == curr_pos: if closest is None or dist(i, j) < dist(*closest): closest = i, j if closest is None: break curr_pos = closest collected += 1 print(collected)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER FUNC_DEF RETURN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR STRING VAR VAR VAR IF VAR NONE FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't. The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers H and W (1 ≀ H, W ≀ 5), separated by a space, β€” the height and the width of the cake. The next H lines contain a string of W characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
def clouded_strategy(H, W, cake): ans = int(cake[0][0] == "*") i, j = 0, 0 prev = "S" while (i, j) != (H - 1, W - 1): if i == H - 1 or j == W - 1: if i == H - 1 and j != W - 1: j += 1 if j == W - 1 and i != H - 1: i += 1 ans += int(cake[i][j] == "*") elif cake[i][j + 1] != "*" and cake[i + 1][j] != "*": if prev == "E": i, j = i + 1, j prev == "S" if prev == "S": i, j = i, j + 1 prev == "E" elif cake[i][j + 1] == "*": i, j = i, j + 1 prev == "E" ans += 1 else: i, j = i + 1, j prev == "S" ans += 1 print(ans) H, W = map(int, input().split(" ")) cake = [[] for i in range(H)] for i in range(H): cake[i] = list(input()) clouded_strategy(H, W, cake)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING WHILE VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR STRING IF VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR VAR STRING IF VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR VAR STRING VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) a = list(map(int, input().split())) sum = 0 for x in a: sum += x t = sum // n ans = 0 for i in range(n - 1): if a[i] < t: ans += t - a[i] a[i + 1] -= t - a[i] else: ans += a[i] - t a[i + 1] += a[i] - t print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) a = [int(i) for i in input().split()] av = sum(a) // len(a) ans = 0 s = 0 for i in a: s += i - av ans += abs(s) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) a = list(map(int, input().split())) k = sum(a) s = 0 r = 0 for i in range(n): s += k // n - a[i] r += abs(s) print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) arr = list(map(int, input().split())) k = sum(arr) // n moves = 0 for i in range(n - 1): arr[i + 1] += arr[i] - k moves += abs(k - arr[i]) print(moves)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) a = list(map(int, input().split())) + [0] s = sum(a) // n d = 0 for i in range(n): if a[i] > s: a[i + 1] += abs(a[i] - s) else: a[i + 1] -= abs(a[i] - s) d += abs(a[i] - s) print(d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) s = [int(i) for i in input().split()] k = sum(s) // n c = 0 for i in range(0, len(s) - 1): if s[i] == k: continue need = k - s[i] s[i + 1] -= need c += abs(need) print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
def read(mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return [int(x) for x in inputs.split()] def write(s="\n"): if isinstance(s, list): s = " ".join(s) s = str(s) print(s, end="") (n,) = read() a = read() s = sum(a) // n r = 0 for i in range(n - 1): if a[i] < s: r += s - a[i] a[i + 1] -= s - a[i] else: r += a[i] - s a[i + 1] += a[i] - s print(r)
FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
import itertools n = int(input()) a = map(int, input().split()) a = list(itertools.accumulate(a)) need_one = a[-1] // n ans = 0 for i in range(len(a) - 1): ans += abs((i + 1) * need_one - a[i]) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) lst = [*map(int, input().split())] item = sum(lst) // n if n == 1: print(0) return less, more, leng1, leng2 = [], [], 0, 0 for i, x in enumerate(lst): if x < item: less.append(i) leng1 += 1 if x > item: more.append(i) leng2 += 1 if leng1 == 0 and leng2 == 0: print(0) return i, j = 0, 0 x, y = less[i], more[j] res = 0 while i < leng1 and j < leng2: x, y = less[i], more[j] elem1 = lst[x] elem2 = lst[y] diff1 = item - elem1 diff2 = elem2 - item if diff2 > diff1: res += abs(x - y) * diff1 lst[y] -= diff1 i += 1 elif diff2 < diff1: res += abs(x - y) * diff2 lst[x] += diff2 j += 1 else: res += abs(x - y) * diff1 i, j = i + 1, j + 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR LIST LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) boxs = list(map(int, input().split())) tot = sum(boxs) num = tot // n s = 0 for i in range(n - 1): if boxs[i] < num: boxs[i + 1] = boxs[i + 1] - (num - boxs[i]) s += num - boxs[i] boxs[i] = num elif boxs[i] > num: boxs[i + 1] = boxs[i + 1] + (boxs[i] - num) s += boxs[i] - num boxs[i] = num print(int(s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) a = list(map(int, input().split())) t = 0 for i in range(n): t += a[i] t = t // n cnt = 0 for i in range(n): if a[i] > t: a[i + 1] += abs(a[i] - t) cnt += abs(a[i] - t) elif a[i] < t: a[i + 1] -= abs(a[i] - t) cnt += abs(a[i] - t) print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) t = list(map(int, input().split())) s, k = 0, sum(t) // n t = [(i - k) for i in t] for i in range(n - 1): s += abs(t[i]) t[i + 1] += t[i] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
x = int(input()) a = list(map(int, input().strip().split(" "))) count = 0 sum = 0 for i in range(len(a)): sum += a[i] z = int(sum / x) for i in range(len(a)): if a[i] != z and a[i] > z: b = abs(z - a[i]) a[i] = a[i] - b count += b a[i + 1] += b if a[i] != z and a[i] < z: b = abs(z - a[i]) a[i] = a[i] + b count += b a[i + 1] -= b print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
a = [] def main(): n = int(input()) a.append(0) B = input() for x in B.split(): a.append(int(x)) a.append(0) S = sum(a) ave = int(S / n) ans = 0 for i in range(1, n + 1, 1): if a[i] < ave: ans += ave - a[i] a[i + 1] -= ave - a[i] else: ans += a[i] - ave a[i + 1] += a[i] - ave print(ans) main()
ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration? -----Input----- The first line contains integer n (1 ≀ n ≀ 50000). The second line contains n non-negative numbers that do not exceed 10^9, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n. -----Output----- Print the total minimum number of moves. -----Examples----- Input 6 1 6 2 5 3 7 Output 12
n = int(input()) a = list(map(int, input().split())) k = 0 for i in range(n): k += a[i] aver = k // n h = 0 x = 0 y = 1 s = 0 for i in range(n): s += aver - a[i] h += abs(s) print(h)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) for _ in range(t): n = int(input()) S = input() if S[0] == "0" or S[-1] == "0": print("NO") continue cnt = 0 for s in S: if s == "1": cnt += 1 if cnt & 1: print("NO") continue k = 0 a = "" b = "" flip = 1 for s in S: if s == "1": k += 1 a += "(" if 2 * k <= cnt else ")" b += a[-1] else: a += "(" if flip else ")" b += ")" if flip else "(" flip ^= 1 print("YES") print(a) 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 IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR BIN_OP NUMBER VAR VAR STRING STRING VAR VAR NUMBER VAR VAR STRING STRING VAR VAR STRING STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
inn = lambda: int(input()) inm = lambda: map(int, input().split()) ins = lambda: str(input()) ina = lambda: list(map(int, input().split())) def solve(): n = inn() ones = 0 zeros = 0 s = ins() for i in s: if i == "1": ones += 1 else: zeros += 1 if ones % 2 == 0 and zeros % 2 == 0: a = "" b = "" o = ones z = 0 for i in s: if i == "1": if o > ones // 2: a += "(" b += "(" else: a += ")" b += ")" o -= 1 else: if z == 0: a += "(" b += ")" else: a += ")" b += "(" z = 1 - z def check(t): cnt = 0 for i in t: if i == "(": cnt += 1 else: cnt -= 1 if cnt < 0: return False if cnt: return False return True if check(a) and check(b): print("YES") print(a) print(b) else: print("NO") else: print("NO") def main(): t = 1 t = int(input()) for _ in range(t): solve() main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
import sys input = sys.stdin.readline YES = "YES" NO = "NO" def solve(N, S): if S[0] == 0 or S[-1] == 0: print(NO) return A = ["("] B = ["("] current_a = 1 current_b = 1 for s in S[1:-1]: if s: if current_a + current_b == 2: current_a += 1 current_b += 1 A.append("(") B.append("(") else: assert current_a > 0 assert current_b > 0 current_a -= 1 current_b -= 1 A.append(")") B.append(")") elif current_b - current_a == 2: assert current_b > 0 current_a += 1 current_b -= 1 A.append("(") B.append(")") else: assert current_a > 0 current_a -= 1 current_b += 1 A.append(")") B.append("(") if current_a != 1 or current_b != 1: print(NO) return A.append(")") B.append(")") print(YES) print("".join(A)) print("".join(B)) def main(): T = int(input()) for _ in range(T): N = int(input()) S = list(map(int, input()[:-1])) solve(N, S) main()
IMPORT ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR LIST STRING ASSIGN VAR LIST STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER NUMBER IF VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF 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 NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
def readInt(): return int(input()) def readInts(): return [int(x) for x in input().split()] def readString(): return input().rstrip() def readCase(): n = readInt() a = readString() return n, a def solve(n, a): ones = a.count("1") if ones % 2 == 1: return "NO" first, second = "", "" first_open, second_open = 0, 0 ones_open_limit = ones // 2 ones_used = 0 for bit in a: if bit == "1": if ones_used < ones_open_limit: first += "(" second += "(" first_open += 1 second_open += 1 else: first += ")" second += ")" first_open -= 1 second_open -= 1 ones_used += 1 elif first_open <= second_open: first += "(" first_open += 1 second += ")" second_open -= 1 else: first += ")" first_open -= 1 second += "(" second_open += 1 if first_open < 0 or second_open < 0: return "NO" return "\n".join(["YES", first, second]) cases = readInt() for case in range(cases): print(solve(*readCase()))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER RETURN STRING ASSIGN VAR VAR STRING STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR VAR VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN STRING RETURN FUNC_CALL STRING LIST STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
import sys input = sys.stdin.readline for f in range(int(input())): n = int(input()) a = [0] * n b = [0] * n ao = bo = 0 poss = True s = input() for i in range(n): if s[i] == "1": if ao == 0 or bo == 0 or ao == bo == 1 and i < n - 1: ao += 1 bo += 1 a[i] = "(" b[i] = "(" else: ao -= 1 bo -= 1 a[i] = ")" b[i] = ")" elif ao > bo: ao -= 1 bo += 1 a[i] = ")" b[i] = "(" elif bo > 0: bo -= 1 ao += 1 a[i] = "(" b[i] = ")" else: poss = False if ao == bo == 0 and poss: print("YES") print(*a, sep="") print(*b, sep="") else: print("NO")
IMPORT ASSIGN VAR VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) def valid(s): tmp = 0 for c in s: if c == "(": tmp += 1 else: tmp -= 1 if tmp < 0: return False return True for _ in range(t): n = int(input()) s = input() a, b = "", "" c1 = s.count("1") if c1 % 2 or n % 2: print("NO") continue c1 //= 2 t1 = 0 t = 0 for c in s: if c == "1": if t1 < c1: a += "(" b += "(" else: a += ")" b += ")" t1 += 1 else: if t == 0: a += "(" b += ")" else: a += ")" b += "(" t ^= 1 if valid(a) and valid(b): print("YES") print(a) print(b) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR VAR VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) s = input() one, zero = s.count("1"), s.count("0") if s[0] == "0" or s[n - 1] == "0" or zero % 2 or n % 2: print("NO") continue curr_one, curr_zero = 0, 0 a = "" for i in range(n): if s[i] == "1": if curr_one < one // 2: a += "(" else: a += ")" curr_one += 1 else: if curr_zero % 2: a += ")" else: a += "(" curr_zero += 1 b = "" for i in range(n): if s[i] == "1": b += a[i] elif a[i] == "(": b += ")" else: b += "(" print("YES") print(a) print(b)
IMPORT 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 VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR STRING VAR STRING VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR IF VAR VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) for i in range(t): n = int(input()) s = input() o = 0 a = "" b = "" z = 0 to = 0 tz = 0 if s[0] == "0" or s[n - 1] == "0": print("NO") else: for i in range(n): if s[i] == "1": o += 1 else: z += 1 if o % 2 != 0 or z % 2 != 0: print("NO") else: print("YES") for i in range(n): if s[i] == "1": to += 1 if to <= o // 2: a += "(" else: a += ")" else: tz += 1 if tz % 2 != 0: a += "(" else: a += ")" print(a) for i in range(n): if s[i] == "1": b += a[i] elif a[i] == "(": b += ")" else: b += "(" 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 ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR IF VAR VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline for _ in range(int(input())): N = int(input()) S = input().rstrip("\n") A = [] B = [] cnt_A = 0 cnt_B = 0 num_1 = S.count("1") if num_1 & 1: print("NO") continue cnt_1 = 0 flg = 0 for i, s in enumerate(S): if s == "1": if cnt_1 < num_1 // 2: A.append("(") B.append("(") cnt_A += 1 cnt_B += 1 else: A.append(")") B.append(")") cnt_A -= 1 cnt_B -= 1 cnt_1 += 1 else: if flg: A.append(")") cnt_A -= 1 B.append("(") cnt_B += 1 else: A.append("(") cnt_A += 1 B.append(")") cnt_B -= 1 flg ^= 1 if cnt_A < 0 or cnt_B < 0: break if cnt_A == cnt_B == 0: print("YES") print("".join(A)) print("".join(B)) else: print("NO") main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT 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 STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
for _ in range(int(input())): n = int(input()) s = input() k = s.count("1") m = s.count("0") if s[0] == "0" or s[n - 1] == "0" or m % 2 != 0 or k % 2 != 0: print("NO") continue a = "" b = "" d = 0 c = 0 for i in s: if i == "1": if d < k / 2: a += "(" b += "(" else: a += ")" b += ")" d += 1 else: if c % 2 == 0: a += ")" b += "(" else: a += "(" b += ")" c += 1 print("YES") print(a) 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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) for i in range(t): n = int(input()) s = input() cnt1 = s.count("1") cnt0 = n - cnt1 a = [] b = [] if cnt1 % 2 == 0 and cnt0 % 2 == 0 and s[0] == s[-1] == "1": j = 0 pnt = 0 flip = 0 while pnt < cnt1 // 2: if s[j] == "1": a.append("(") b.append("(") pnt += 1 elif flip: a.append(")") b.append("(") flip = 0 else: a.append("(") b.append(")") flip = 1 j += 1 while pnt < cnt1: if s[j] == "1": a.append(")") b.append(")") pnt += 1 elif flip: a.append(")") b.append("(") flip = 0 else: a.append("(") b.append(")") flip = 1 j += 1 print("YES") print("".join(a)) print("".join(b)) else: print("NO")
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 STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
ll = lambda: map(int, input().split()) t = lambda: int(input()) ss = lambda: input() lx = lambda x: map(int, input().split(x)) for s in [*open(0)][2::2]: N = len(s) - 1 o = s.count("1") if s[0] < "1" or s[-2] < "1": print("NO") continue if o & 1: print("NO") continue n = 0 a = [0] * N b = [0] * N c = 1 for i in range(N): if s[i] < "1": if c: a[i] = ")" b[i] = "(" else: a[i] = "(" b[i] = ")" c ^= 1 else: n += 1 if 2 * n > o: a[i] = b[i] = ")" n += 1 else: a[i] = b[i] = "(" print("YES\n" + "".join(a) + "\n" + "".join(b))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING FUNC_CALL STRING VAR STRING FUNC_CALL STRING VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
import sys input = sys.stdin.readline def check(K): z = 0 for k in K: if k == "(": z += 1 else: z -= 1 if z < 0: return 0 return 1 t = int(input()) for tests in range(t): n = int(input()) s = input().strip() ONE = s.count("1") if ONE % 2 != 0: print("NO") continue A = [0] * n B = [0] * n z = 0 o = 0 for i in range(n): if s[i] == "1": o += 1 if o <= ONE // 2: A[i] = "(" B[i] = "(" else: A[i] = ")" B[i] = ")" else: if z % 2 == 0: A[i] = "(" B[i] = ")" else: A[i] = ")" B[i] = "(" z += 1 if check(A) == 1 and check(B) == 1: print("YES") print("".join(A)) print("".join(B)) else: print("NO")
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER 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 STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
def solve(): n = int(input()) s = input() one = s.count("1") zero = s.count("0") if one % 2 == 1 or zero % 2 == 1 or s[0] == "0" or s[n - 1] == "0": print("NO") return a = "" b = "" for i in s: if i == "1": if one: a += "(" b += "(" one -= 2 else: a += ")" b += ")" elif zero % 2 == 0: a += "(" b += ")" zero -= 1 else: a += ")" b += "(" zero -= 1 print("YES") print(a) print(b) t = int(input()) while t > 0: t -= 1 solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR STRING IF VAR VAR STRING VAR STRING VAR NUMBER VAR STRING VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR NUMBER VAR STRING VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) for i in range(t): n = int(input()) s = input() z = s.count("0") o = s.count("1") a, b = "", "" if s[0] == "1" and s[n - 1] == "1" and o % 2 == 0 and z % 2 == 0: one = 0 zero = 0 for i in s: if i == "1": one += 1 if one <= o // 2: a += "(" b += "(" else: a += ")" b += ")" else: zero += 1 if zero % 2 == 0: a += "(" b += ")" else: a += ")" b += "(" print("YES") print(a) print(b) else: print("NO")
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 STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR STRING STRING IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
T = int(input()) for t in range(T): n = int(input()) s = input() can = True if s.count("0") % 2 == 1: can = False elif n % 2 == 1: can = False elif s[0] == "0" or s[-1] == "0": can = False if not can: print("NO") continue parity = 0 pdict = {(0): "()", (1): ")("} a = [] b = [] h = 0 for i in range(n): if s[i] == "0": a.append(pdict[parity][0]) b.append(pdict[parity][1]) parity = 1 - parity elif i == n - 1 or h >= 2: a.append(")") b.append(")") h -= 1 else: a.append("(") b.append("(") h += 1 print("YES") print("".join(a)) print("".join(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 ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER STRING STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
for _ in range(int(input())): n = int(input()) s = input() if s[0] == "0" or s[-1] == "0" or s.count("1") % 2 == 1: print("NO") else: ok = True a, b = [0] * n, [0] * n i = 1 last = "(" k = s.count("1") // 2 for i in range(n): if k == 0: last = ")" if s[i] == "1": a[i] = last b[i] = last k -= 1 last = "(" l2 = ")" for i in range(n): if s[i] == "0": a[i] = last b[i] = l2 l2, last = last, l2 print("YES") print("".join(a)) print("".join(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 IF VAR NUMBER STRING VAR NUMBER STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR STRING IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
import sys input = lambda: sys.stdin.readline().rstrip() T = int(input()) for _ in range(T): N = int(input()) X = [int(a) for a in input()] s = sum(X) if s % 2 or X[0] == 0 or X[-1] == 0: print("NO") continue A = [0] * N B = [0] * N k = 0 l = 1 for i, x in enumerate(X): if x: k += 1 if k * 2 <= s: A[i] = B[i] = 1 else: A[i] = l l ^= 1 B[i] = l print("YES") print("".join([("(" if a else ")") for a in A])) print("".join([("(" if a else ")") for a in B]))
IMPORT 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 VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING STRING VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) while t > 0: n = int(input()) s = input() a = "" b = "" one = 0 zero = 0 for i in s: if i is "1": one += 1 elif i is "0": zero += 1 if zero % 2 == 1 or one % 2 == 1 or s[0] == "0" or s[-1] == "0": print("NO") else: for i in s: if i == "1": if one: a += "(" b += "(" one -= 2 else: a += ")" b += ")" elif zero % 2 == 0: a += ")" b += "(" zero -= 1 else: a += "(" b += ")" zero -= 1 print("YES") print(a) print(b) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR IF VAR STRING IF VAR VAR STRING VAR STRING VAR NUMBER VAR STRING VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR NUMBER VAR STRING VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) for i in range(t): n = int(input()) s = str(input()) res1 = "" res2 = "" zero = s.count("0") one = s.count("1") a = one / 2 if s[0] == "0" or s[n - 1] == "0" or zero % 2 == 1 or one % 2 == 1: print("no") else: a = int(one / 2) u = 0 v = 0 for j in range(n): if s[j] == "1": u += 1 if u <= a: res1 = res1 + "(" res2 = res2 + "(" else: res1 = res1 + ")" res2 = res2 + ")" elif v == 0: res1 = res1 + "(" res2 = res2 + ")" v += 1 else: res1 = res1 + ")" res2 = res2 + "(" v -= 1 print("yes") print(res1) print(res2)
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 STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
import sys from sys import stdin tt = int(stdin.readline()) for loop in range(tt): n = int(stdin.readline()) s = stdin.readline()[:-1] S = s if n % 2 == 1: print("NO") continue a = [] b = [] at = 0 bt = 0 znum = 0 onum = 0 for i in range(n): if s[i] == "1": onum += 1 else: znum += 1 if znum % 2 == 1: print("NO") continue nz = 0 no = 0 at = 0 bt = 0 a = [] b = [] ans = "YES" for i in range(n): if S[i] == "1": no += 1 if no * 2 > onum: a.append(")") b.append(")") at -= 1 bt -= 1 else: a.append("(") b.append("(") at += 1 bt += 1 else: if nz % 2 == 0: a.append("(") b.append(")") at += 1 bt -= 1 else: a.append(")") b.append("(") at -= 1 bt += 1 nz += 1 if at < 0 or bt < 0: ans = "NO" break if at != 0 or bt != 0 or ans == "NO": print("NO") else: print("YES") print("".join(a)) print("".join(b))
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 NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
for _ in range(int(input())): n = int(input()) string = input() if string[0] != "1" or string[-1] != "1": print("NO") continue else: ones = string.count("1") zeros = n - ones if ones % 2 == 1: print("NO") continue output1 = [-1] * n output2 = [-1] * n countOne = 0 g = 0 for i in range(n): if string[i] == "1": if countOne < ones / 2: output1[i] = "(" output2[i] = "(" else: output1[i] = ")" output2[i] = ")" countOne += 1 else: if g == 0: output1[i] = "(" output2[i] = ")" else: output1[i] = ")" output2[i] = "(" g ^= 1 print("YES") print("".join(output1)) print("".join(output2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
for i in range(int(input())): N = int(input()) s = input() zero = 0 for i in range(N): if s[i] == "0": zero += 1 if zero % 2: print("NO") elif s[0] == "0" or s[-1] == "0": print("NO") else: one = N - zero s1 = "" s2 = "" one_half = one / 2 flag_0 = 0 for i in range(N): if s[i] == "1" and one_half > 0: s1 += "(" s2 += "(" one_half -= 1 elif s[i] == "1" and one_half == 0: s1 += ")" s2 += ")" elif s[i] == "0" and flag_0 == 0: s1 += "(" s2 += ")" flag_0 = 1 elif s[i] == "0" and flag_0 == 1: s1 += ")" s2 += "(" flag_0 = 0 print("YES") print(s1) print(s2)
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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR STRING VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR STRING VAR STRING IF VAR VAR STRING VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
for _ in range(int(input())): n = int(input()) s = list(map(str, input())) if n % 2 != 0: print("NO") elif s[0] == "0" or s[n - 1] == "0": print("NO") elif s.count("0") % 2 != 0: print("NO") else: a = "(" b = ")" s1 = list.copy(s) s2 = list.copy(s) mid = s.count("1") // 2 j = 1 for i in range(n): if j <= mid: if s1[i] == "1": s1[i] = "(" s2[i] = "(" j += 1 else: s1[i] = a s2[i] = b a, b = b, a elif s1[i] == "1": s1[i] = ")" s2[i] = ")" else: s1[i] = a s2[i] = b a, b = b, a print("YES") print("".join(s1)) print("".join(s2))
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 IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
from sys import stdin, stdout t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) s = stdin.readline(n) stdin.readline(1) if s[0] == "0" or s[n - 1] == "0": stdout.write("NO\n") continue a = [] b = [] va, vb = 0, 0 for i in range(n): if s[i] == "1": if va + vb < 4: va += 1 vb += 1 a.append("(") b.append("(") else: va -= 1 vb -= 1 a.append(")") b.append(")") elif s[i] == "0": if va <= 1: va += 1 vb -= 1 a.append("(") b.append(")") else: va -= 1 vb += 1 a.append(")") b.append("(") if not va == vb == 2: stdout.write("NO\n") else: a[-1] = ")" b[-1] = ")" stdout.write("YES\n") stdout.write("".join(a)) stdout.write("\n") stdout.write("".join(b)) stdout.write("\n")
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 EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
def ss(n, s): a = "" b = "" x = 0 y = 0 p = s.count("1") q = 0 f = 0 for i in s: if i == "1": p -= 1 q += 1 if q > p: a += ")" b += ")" x -= 1 y -= 1 else: a += "(" b += "(" x += 1 y += 1 elif f: f = 0 a += ")" b += "(" x -= 1 y += 1 else: f = 1 a += "(" b += ")" x += 1 y -= 1 if x < 0 or y < 0: return [-1] if x == 0 and y == 0: return [a, b] else: return [-1] for _ in range(int(input())): n = int(input()) s = str(input()) n = len(s) if n % 2 == 1: print("NO") else: ans = ss(n, s) if ans == [-1]: print("NO") else: print("YES") print(ans[0]) print(ans[1])
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN LIST NUMBER IF VAR NUMBER VAR NUMBER RETURN LIST VAR VAR RETURN LIST 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR LIST NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
def solve(n, s): cnt = 0 for i in s: if i == "1": cnt += 1 if cnt % 2 == 1 or s[0] == "0" or s[-1] == "0": print("NO") return k = 0 flip = 0 a = "" b = "" for i in range(n): if s[i] == "1": if 2 * k < cnt: a += "(" else: a += ")" b += a[-1] k += 1 else: if flip == 1: a += "(" b += ")" else: a += ")" b += "(" flip = not flip print("YES") print(a) print(b) return for t in range(int(input())): n = int(input()) s = input() solve(n, s)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP NUMBER VAR VAR VAR STRING VAR STRING VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) for _ in range(t): n = int(input()) s = input() s = list(s) a = "" b = "" c1 = 0 c2 = False c = 0 d = 0 for i in range(n): if s[i] == "1": c += 1 if s[0] == "0" or s[-1] == "0" or c % 2 == 1: print("NO") d = 1 continue for i in range(n): if s[i] == "1": if 2 * c1 < c: a = a + "(" b += "(" else: a += ")" b += ")" c1 += 1 else: if not c2: a += "(" b += ")" else: a += ")" b += "(" c2 = not c2 if d == 0: print("YES") print(a) 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
rn = lambda: int(input()) rns = lambda: map(int, input().split()) rl = lambda: list(map(int, input().split())) rs = lambda: input() YN = lambda x: print("YES") if x else print("NO") mod = 10**9 + 7 for _ in range(rn()): n = rn() s = rs() c = s.count("1") if s[0] == s[-1] == "1" and c % 2 == 0: print("YES") a = "" b = "" turn = 1 ones = 0 for i in range(n): if s[i] == "0": if turn == 1: a += "(" b += ")" else: a += ")" b += "(" turn *= -1 elif ones < c // 2: a += "(" b += "(" ones += 1 else: a += ")" b += ")" print(a) print(b) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
for t in range(int(input())): n = int(input()) s = input() ans = "YES" cnt = s.count("1") if s[0] == "0" or s[-1] == "0" or cnt % 2 == 1: ans = "NO" else: a, b = "", "" check = 0 temp = False for i in range(n): if s[i] == "0": if temp: a += ")" b += "(" else: a += "(" b += ")" temp = not temp else: if check * 2 < cnt: a += "(" b += "(" else: a += ")" b += ")" check += 1 print(ans) if ans == "YES": print(a) 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 ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR VAR STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
for _ in range(int(input())): n = int(input()) s = input() if s[0] == s[-1] == "0" or s[0] != s[-1]: print("NO") continue if s.count("0") % 2: print("NO") continue cnt = s.count("1") k = 0 a = "" b = "" flip = True for i in s: if i == "1": if 2 * k < cnt: a += "(" else: a += ")" b += a[-1] k += 1 else: if flip: a += "(" b += ")" else: a += ")" b += "(" flip = not flip print("YES") print(a) 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 IF VAR NUMBER VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF BIN_OP NUMBER VAR VAR VAR STRING VAR STRING VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
for test in range(int(input())): n = int(input()) s = input() cnt = s.count("1") if cnt & 1 or s[0] == "0" or s[-1] == "0": print("NO") continue c = 1 k = 0 a = "" b = "" for i in range(n): if s[i] == "1": if 2 * k < cnt: a += "(" b += a[-1] else: a += ")" b += a[-1] k += 1 else: if c: a += "(" b += ")" else: a += ")" b += "(" c ^= 1 print("YES") print(a) 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 ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP NUMBER VAR VAR VAR STRING VAR VAR NUMBER VAR STRING VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
def answer(s): count1 = 0 count = 0 count0 = 0 a = "" b = "" if s[0] == "0" or s[-1] == "0": return ["NO"] for i in s: if i == "1": count1 += 1 else: count0 += 1 if count1 % 2 != 0 or count1 == 0 or len(s) % 2 != 0: return ["NO"] var = 0 for i in s: if i == "1": if count < count1 // 2: a += "(" b += "(" else: a += ")" b += ")" count += 1 elif var == 0: a += ")" b += "(" var = 1 else: a += "(" b += ")" var = 0 return ["YES", a, b] t = int(input()) for w in range(t): n = int(input()) s = input() result = answer(s) if result[0] == "NO": print("NO") else: print("YES") print(result[1]) print(result[2])
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING RETURN LIST STRING FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN LIST STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER RETURN LIST STRING 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 FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) s = input() a = [] b = [] count1 = 0 for i in range(n): if s[i] == "1": count1 += 1 count0 = n - count1 if count0 % 2: print("NO") continue leftbr0 = count0 // 2 rightbr0 = count0 // 2 leftbr1 = n // 2 - leftbr0 score = 0 stack = [] for i in range(n): if s[i] == "1": if leftbr1: a.append("(") score += 1 leftbr1 -= 1 else: a.append(")") score -= 1 if score < 0 and stack: rightbr0 += 1 score += 2 k = stack.pop() a[k] = "(" elif rightbr0 and score > 0: a.append(")") score -= 1 rightbr0 -= 1 stack.append(i) else: a.append("(") score += 1 b = [] for i in range(n): if s[i] == "1": b.append(a[i]) elif a[i] == "(": b.append(")") else: b.append("(") score1 = 0 score2 = 0 flag = 0 for i in range(n): if a[i] == "(": score1 += 1 else: score1 -= 1 if b[i] == "(": score2 += 1 else: score2 -= 1 if score1 < 0 or score2 < 0: flag = 1 break if flag or score1 != 0 or score2 != 0: print("NO") else: print("YES") print("".join(a)) print("".join(b))
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 VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR STRING IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) for i in range(t): n = int(input()) s = input() a = "" b = "" c1 = s.count("1") c0 = s.count("0") if c1 % 2 != 0 or c0 % 2 != 0 or s[0] != "1" or s[-1] != "1": print("NO") else: print("YES") one = 0 zer = 0 for j in range(n): if s[j] == "1": one += 1 if one <= c1 // 2: a += "(" b += "(" else: a += ")" b += ")" else: zer += 1 if zer % 2 != 0: a += "(" b += ")" else: a += ")" b += "(" print(a) 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 ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
def isvalid(s): c = 0 for i in range(len(s)): if s[i] == "(": c += 1 else: c -= 1 if c < 0: return False if c == 0: return True return False t = int(input()) while t != 0: n = int(input()) s = input() if n & 1 == 1: print("NO") else: zero = 0 for i in range(n): if s[i] == "0": zero += 1 if zero % 2 == 1: print("NO") else: count = (n - zero) // 2 s1 = "" s2 = "" f = 0 for i in range(n): if s[i] == "1" and count > 0: s1 += "(" s2 += "(" count -= 1 elif s[i] == "1": s1 += ")" s2 += ")" if s[i] == "0" and f == 0: s1 += "(" s2 += ")" f = 1 elif s[i] == "0": s1 += ")" s2 += "(" f = 0 if isvalid(s1) and isvalid(s2): print("YES") print(s1) print(s2) else: print("NO") t -= 1
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR STRING VAR STRING VAR NUMBER IF VAR VAR STRING VAR STRING VAR STRING IF VAR VAR STRING VAR NUMBER VAR STRING VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR STRING VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
def solve(s, n): cnt = 0 for x in s: cnt += x == "1" c0 = n - cnt if cnt % 2 == 1: print("NO") return cnt //= 2 a = b = "" sa = sb = 0 for i in range(n): if s[i] == "1": if cnt > 0: a += "(" b += "(" sa += 1 sb += 1 else: a += ")" b += ")" sa -= 1 sb -= 1 cnt -= 1 else: if c0 % 2 == 1: a += ")" b += "(" sa -= 1 sb += 1 else: a += "(" b += ")" sa += 1 sb -= 1 c0 -= 1 if sa < 0 or sb < 0: print("NO") return print("YES") print(a) print(b) t = int(input()) for _ in range(t): n = int(input()) s = input() solve(s, n)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING 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
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
Time = int(input()) def solve(): N = int(input()) S = input() cnt = S.count("1") if S[0] == "0" or S[-1] == "0" or cnt % 2 == 1: print("NO") return A, B = "", "" k = 0 flip = False for i in range(N): if S[i] == "1": if 2 * k < cnt: A += "(" B += "(" else: A += ")" B += ")" k += 1 else: if flip: A += "(" B += ")" else: A += ")" B += "(" flip = not flip print("YES") print(A) print(B) for _ in range(Time): solve()
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 FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF BIN_OP NUMBER VAR VAR VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
def solve(): n = int(input()) ip = input() ones = ip.count("1") zers = ip.count("0") if zers % 2 != 0 or ip[0] == "0" or ip[n - 1] == "0": print("NO") else: ans1 = "" ans2 = "" k = 0 flag = 1 for i in range(n): if ip[i] == "1": if k < ones // 2: ans1 = ans1 + "(" ans2 = ans2 + "(" else: ans1 = ans1 + ")" ans2 = ans2 + ")" k += 1 else: if flag == 1: ans1 += "(" else: ans1 += ")" if flag == 1: ans2 += ")" else: ans2 += "(" if flag == 1: flag = 0 else: flag = 1 print("YES") print(ans1) print(ans2) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING IF VAR NUMBER VAR STRING VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
try: def solve(n, s): add = 0 for i in s: if i == "1": add += 1 if s[0] == "0" or s[n - 1] == "0" or add % 2 == 1: print("NO") return a, b = "", "" c1, c2 = 1, 0 for i in s: if i == "1": if c1 <= add // 2: a += "(" b += "(" else: a += ")" b += ")" c1 += 1 else: if c2 % 2 == 0: a += "(" b += ")" else: a += ")" b += "(" c2 += 1 print("YES") print(a) print(b) return t = int(input()) while t: n = int(input()) s = input() solve(n, s) t -= 1 except EOFError as e: pass
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR STRING STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN 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 VAR VAR VAR NUMBER VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) for t1 in range(0, t): n = int(input()) s = input() if s[0] == "0" or s[-1] == "0": print("NO") else: same = [] diff = [] for i in range(0, n): if s[i] == "1": same.append(i) else: diff.append(i) ans1 = list(s) ans2 = list(s) if len(diff) % 2 == 1: print("NO") else: flag = 1 for i in diff: if flag == 1: ans1[i] = ")" ans2[i] = "(" else: ans1[i] = "(" ans2[i] = ")" flag = (flag + 1) % 2 start = 0 end = len(same) - 1 while start < end: xx = same[start] yy = same[end] ans1[xx] = "(" ans2[xx] = "(" ans1[yy] = ")" ans2[yy] = ")" start += 1 end -= 1 print("YES") print("".join(ans1)) print("".join(ans2))
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 IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
t = int(input()) while t: n = int(input()) s = input() o = s.count("1") if s[0] == "0" or s[-1] == "0" or s.count("0") % 2 != 0 or o % 2 != 0: print("NO") else: a = "" b = "" open_flag = 1 p = o // 2 for x in s: if x == "1": if p > 0: a += "(" b += "(" p -= 1 else: a += ")" b += ")" else: if open_flag: a += ")" b += "(" else: a += "(" b += ")" open_flag = 0 if open_flag else 1 print("YES") print(a) print(b) 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 FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING BIN_OP FUNC_CALL VAR STRING NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR STRING VAR STRING IF VAR VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $s$ of length $n$. Construct two balanced bracket sequences $a$ and $b$ of length $n$ such that for all $1\le i\le n$: if $s_i=1$, then $a_i=b_i$ if $s_i=0$, then $a_i\ne b_i$ If it is impossible, you should report about it. -----Input----- The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$, $n$ is even). The next line contains a string $s$ of length $n$, consisting of characters 0 and 1. The sum of $n$ across all test cases does not exceed $2\cdot 10^5$. -----Output----- If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $a$ and $b$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. -----Examples----- Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO -----Note----- In the first test case, $a=$"()()()" and $b=$"((()))". The characters are equal in positions $1$, $3$, $4$, and $6$, which are the exact same positions where $s_i=1$. In the second test case, $a=$"()()((()))" and $b=$"(())()()()". The characters are equal in positions $1$, $4$, $5$, $7$, $8$, $10$, which are the exact same positions where $s_i=1$. In the third test case, there is no solution.
testcases = int(input()) while testcases > 0: n = int(input()) str1 = str(input()) count1 = 0 for a in range(0, n): if str1[a] == "1": count1 += 1 if count1 % 2 == 1 or str1[0] == "0" or str1[n - 1] == "0": print("NO") testcases = testcases - 1 continue flip = True a = "" b = "" k = 0 for i in range(0, n): if str1[i] == "1": temp = "(" if k * 2 < count1 else ")" a = a + temp b = b + temp k += 1 else: temp = ")" if flip else "(" a = a + temp temp2 = "(" if flip else ")" b = b + temp2 flip = not flip print("YES") print(a.strip()) print(b.strip()) testcases = testcases - 1
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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER