description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
n = int(input()) d = {i: [] for i in range(1, n + 1)} for i in range(n - 1): a, b = map(int, input().split()) d[a].append(b) d[b].append(a) if n & 1: print(-1) else: val = [(1) for i in range(n + 1)] par = [(-1) for i in range(n + 1)] par[1] = 1 stack = [1] while stack: u = stack[-1] f = 0 for i in d[u]: if par[i] == -1: par[i] = u stack.append(i) f = 1 if f == 0: stack.pop() val[par[u]] += val[u] ans = 0 for i in range(2, n + 1): if val[i] % 2 == 0: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
import sys Ri = lambda: [int(x) for x in sys.stdin.readline().split()] ri = lambda: sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**18 MOD = 10**9 + 7 n = int(ri()) lis = [[] for i in range(n)] for _ in range(n - 1): a, b = Ri() a -= 1 b -= 1 lis[a].append(b) lis[b].append(a) visit = [-1] * n no = [1] * n sta = [0] visit[0] = True if n & 1 == 1: print(-1) else: while len(sta) > 0: top = sta[-1] ret = True for i in range(len(lis[top])): if visit[lis[top][i]] == -1: visit[lis[top][i]] = top ret = False sta.append(lis[top][i]) if ret: if top != 0: no[visit[top]] += no[top] sta.pop() ans = 0 no = no[1:] for i in no: if i & 1 == 0: ans += 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR IF VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
n = int(input()) if n % 2 == 1: print(-1) exit() adj = [[] for _ in range(n + 1)] for _ in range(n - 1): u, v = map(int, input().split()) adj[u].append(v) adj[v].append(u) cnt = [1] * (n + 1) prt = [-1] * (n + 1) prt[1] = 1 stk = [1] while stk: u = stk[-1] k = False for v in adj[u]: if prt[v] == -1: prt[v] = u stk.append(v) k = True if not k: stk.pop() cnt[prt[u]] += cnt[u] res = 0 for j in cnt[2:]: if j % 2 == 0: res += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
from sys import stdin inp = stdin.readline n, ans = int(inp()), 0 if n & 1: print(-1) return g = {i: [set(), 0] for i in range(1, n + 1)} visited = [0] * (n + 1) for _ in range(n - 1): a, b = map(int, inp().split()) g[a][0].add(b) g[b][0].add(a) visited = [0, 1] + [0] * (n - 1) q = [1] p = [0] * (n + 1) while q: v = q.pop() if v != 1 and len(g[v][0]) == 1: g[p[v]][0].remove(v) g[p[v]][1] += g[v][1] + 1 q.append(p[v]) if g[v][1] & 1: ans += 1 continue for i in g[v][0]: if not visited[i]: visited[i] = True p[i] = v q.append(i) if min(g[v][1], g[i][1]) & 1: ans += 1 print(ans)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR LIST FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
def dfs(g, s): visited = set([s]) stack = [s] res = 0 while len(stack) > 0: s = stack.pop() cnt = 0 cnt += 1 visited.add(s) for v in g[s]: if v not in visited: cnt += 1 stack.append(v) if cnt % 2 == 0: res += 1 return res // 2 def dfs2(g, s, n): parent = [0] * (n + 1) counts = [1] * (n + 1) stack = [] stack.append(s) while stack: v = stack[-1] empty_s = True if not parent[v]: parent[v] = 1 for node in g[v]: if not parent[node]: parent[node] = v stack.append(node) empty_s = False if empty_s: stack.pop() counts[parent[v]] += counts[v] return sum([(1) for x in counts[2:] if x % 2 == 0]) def solve(): n = int(input()) tree = [[] for i in range(n + 1)] for i in range(n - 1): u, v = input().split() u, v = int(u), int(v) tree[v].append(u) tree[u].append(v) if n % 2 != 0: print(-1) return print(dfs2(tree, 1, n)) def __starting_point(): solve() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
def dfs_new(adj, n): stack = [(1, 0)] sol = [None for i in range(n + 1)] while not len(stack) == 0: i, parent = stack[-1] flag = True for item in adj[i]: if item == parent: continue if sol[item] == None: stack.append((item, i)) flag = False if flag: rem = 0 tree_size = 1 for item in adj[i]: if item == parent: continue subtree_size, t = sol[item] if subtree_size % 2 == 0: rem += 1 rem += t tree_size += subtree_size sol[i] = [tree_size, rem] stack.pop(-1) return sol[1][1] n = int(input()) adj = [[] for i in range(n + 1)] for _ in range(n - 1): u, v = map(int, input().split()) adj[u].append(v) adj[v].append(u) if n % 2 == 1: print(-1) else: ans = dfs_new(adj, n) print(ans)
FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
n = int(input()) if n % 2 != 0: print(-1) return links = [[1, set()] for i in range(1, n + 1)] W = 0 L = 1 i = 0 while i < n - 1: i += 1 [a, b] = [int(x) for x in input().split()] links[a - 1][L].add(b - 1) links[b - 1][L].add(a - 1) count = sear = cur = 0 while sear < n: li = cur l = links[li] if len(l[L]) != 1: if sear == cur: sear += 1 cur = sear continue mi = l[L].pop() m = links[mi] if l[W] % 2 == 0: count += 1 else: m[W] += 1 m[L].remove(li) if mi < sear: cur = mi else: sear += 1 cur = sear print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
n = int(input()) if n % 2 == 1: print(-1) else: edges = [[] for i in range(n)] for i in range(n - 1): [a, b] = [int(j) for j in input().split()] edges[a - 1].append(b - 1) edges[b - 1].append(a - 1) dfs_stack = [0] comp_size = [(1) for i in range(n)] visited = [(-1) for i in range(n)] visited[0] = 0 while dfs_stack != []: current_node = dfs_stack[-1] can_go_further = False for i in edges[current_node]: if visited[i] == -1: dfs_stack.append(i) visited[i] = current_node can_go_further = True if can_go_further == False: dfs_stack.pop(-1) comp_size[visited[current_node]] += comp_size[current_node] ans = 0 for i in comp_size[1:]: if i % 2 == 0: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER WHILE VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
def dfs(node): par[1] = 1 stack = [1] ans = 0 while stack: k = stack[-1] f = 0 for i in g[k]: if par[i] == -1: par[i] = k stack.append(i) f = 1 if f == 0: k = stack.pop() subtree[par[k]] += subtree[k] for i in subtree[2:]: if i % 2 == 0: ans = ans + 1 print(ans) n = int(input()) g = {} for i in range(1, n + 1): g[i] = [] for i in range(1, n): a, b = map(int, input().split()) g[a].append(b) g[b].append(a) par = [(-1) for i in range(n + 1)] subtree = [(1) for i in range(n + 1)] if n % 2: print(-1) else: dfs(1)
FUNC_DEF ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
n = int(input()) if n % 2 == 1: print(-1) exit(0) graph = {} for i in range(n - 1): v, u = map(int, input().strip().split()) v -= 1 u -= 1 if v in graph: graph[v].append(u) else: graph[v] = [u] if u in graph: graph[u].append(v) else: graph[u] = [v] countArr = [(1) for i in range(n)] s = [0] vis = [(-1) for i in range(n)] while s: curr = s[-1] k = False for i in graph[curr]: if vis[i] == -1: vis[i] = curr s.append(i) k = True if not k: if curr != 0: countArr[vis[curr]] += countArr[curr] s.pop() o = 0 for i in countArr[1:]: if i % 2 == 0: o += 1 print(o)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
from sys import exit def main(): n = int(input()) g = [[] for i in range(n + 5)] vh = [[] for i in range(n + 5)] size = [(1) for i in range(n + 5)] par = [i for i in range(n + 5)] if n % 2 == 1: print(-1) exit() for i in range(n - 1): u, v = map(int, input().split()) g[u].append(v) g[v].append(u) q = [[1, 1]] vh[1].append(1) max_height = 1 while len(q) > 0: u, hu = q.pop() leaf_node = True for v in g[u]: if v == par[u]: continue hv = hu + 1 par[v] = u q.append([v, hv]) vh[hv].append(v) max_height = max(max_height, hv) while max_height > 0: for v in vh[max_height]: size[par[v]] += size[v] max_height -= 1 print(n - 1 - sum(size[i] % 2 for i in range(1, n + 1))) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
n = int(input()) tr = [[] for i in range(n)] for _ in range(n - 1): u, v = map(int, input().split()) tr[u - 1].append(v - 1) tr[v - 1].append(u - 1) if n % 2: print(-1) exit() c = [1] * n par = [-1] * n par[0] = 0 q = [0] while q: ver = q[-1] flag = False for to in tr[ver]: if par[to] == -1: par[to] = ver q.append(to) flag = True if not flag: q.pop() c[par[ver]] += c[ver] ans = 0 for i in range(1, n): if c[i] % 2 == 0: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You're given a tree with $n$ vertices. Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^5$) denoting the size of the tree. The next $n - 1$ lines contain two integers $u$, $v$ ($1 \le u, v \le n$) each, describing the vertices connected by the $i$-th edge. It's guaranteed that the given edges form a tree. -----Output----- Output a single integer $k$ β€” the maximum number of edges that can be removed to leave all connected components with even size, or $-1$ if it is impossible to remove edges in order to satisfy this property. -----Examples----- Input 4 2 4 4 1 3 1 Output 1 Input 3 1 2 1 3 Output -1 Input 10 7 1 8 4 8 10 4 7 6 5 9 3 3 5 2 10 2 5 Output 4 Input 2 1 2 Output 0 -----Note----- In the first example you can remove the edge between vertices $1$ and $4$. The graph after that will have two connected components with two vertices in each. In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $-1$.
def main(): n = int(input()) if n % 2 != 0: print(-1) return graph = [[] for _ in range(n)] for _ in range(n - 1): a, b = list(map(int, input().split())) graph[a - 1].append(b - 1) graph[b - 1].append(a - 1) res = 0 stack = [0] visited = [-1] * n visited[0] = 0 count = [1] * n while stack: s = stack[-1] flag = False for v in graph[s]: if visited[v] == -1: stack.append(v) visited[v] = s flag = True if not flag: stack.pop() if s == 0: break count[visited[s]] += count[s] res = sum([(1 if i % 2 == 0 else 0) for i in count]) print(res - 1) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys from sys import stdin, stdout def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_string(): return sys.stdin.readline().strip() for _ in range(int(input())): n, k = get_ints() a = get_string() b = get_string() acount = [0] * 26 bcount = [0] * 26 for i in a: acount[ord(i) - ord("a")] += 1 for i in b: bcount[ord(i) - ord("a")] += 1 flag = True for i in range(25): extra = acount[i] - bcount[i] if extra < 0 or extra % k: flag = False break acount[i + 1] += extra if flag: print("Yes") else: print("No")
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
for _ in range(int(input())): n, k = map(int, input().split()) a = [0] * 26 for x in input(): a[ord(x) - 97] += 1 for x in input(): a[ord(x) - 97] -= 1 a = [x for x in a if x != 0] t = 0 pas = True for x in a: if abs(x) % k == 0 and t > -1: t += x else: pas = False break print("Yes" if pas else "No")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) a = input() b = input() ac = [0] * 26 bc = [0] * 26 for i in range(n): ac[ord(a[i]) - 97] += 1 bc[ord(b[i]) - 97] += 1 for i in range(26): if ac[i] >= bc[i]: ac[i] -= bc[i] if i + 1 < 26: m = ac[i] // k ac[i] -= m * k ac[i + 1] += m * k else: return "NO" return "YES" t = int(input()) i = 0 while i < t: print(solve()) i += 1
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) def get_list_string(): return list(map(str, sys.stdin.readline().strip().split())) def get_string(): return sys.stdin.readline().strip() def get_int(): return int(sys.stdin.readline().strip()) def get_print_int(x): sys.stdout.write(str(x) + "\n") def get_print(x): sys.stdout.write(x + "\n") def solve(): for _ in range(get_int()): n, k = get_ints() a = get_string() b = get_string() temp1, temp2 = [0] * 26, [0] * 26 for i in a: temp1[ord(i) - 97] += 1 for i in b: temp2[ord(i) - 97] += 1 f = 0 count = 0 for i in range(26): d = temp1[i] - temp2[i] if d == 0: continue if abs(d) % k == 1: f = 1 break count += d // k if count < 0: f = 1 break if f: get_print("No") else: get_print("Yes") solve()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys from sys import stdin t = int(stdin.readline()) alp = "abcdefghijklmnopqrstuvwxyz" ad = {} for i in range(26): ad[alp[i]] = i for i in range(t): n, k = map(int, stdin.readline().split()) a = stdin.readline()[:-1] b = stdin.readline()[:-1] have = [0] * 27 must = [0] * 27 for c in a: have[ad[c]] += 1 for c in b: must[ad[c]] += 1 flag = False nowa = 0 nowb = 0 for i in range(26): nowa += have[i] nowb = must[i] if nowb > nowa: flag = True break nowa -= nowb if nowa % k != 0: flag = True break if flag: print("No") else: print("Yes")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) a, b = input()[:-1], input()[:-1] cnta = [0] * 27 cntb = [0] * 27 for c in a: cnta[ord(c) - ord("a")] += 1 for c in b: cntb[ord(c) - ord("a")] += 1 for i in range(26): if cnta[i] < cntb[i] or (cntb[i] - cnta[i]) % k: print("No") break cnta[i + 1] += cnta[i] - cntb[i] else: print("Yes")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
from sys import stdin input = stdin.readline for _ in range(int(input())): a, b = map(int, input().split()) s1 = list(input().strip()) s2 = list(input().strip()) c1, c2 = [0] * 26, [0] * 26 for i in range(a): c1[ord(s1[i]) - 97] += 1 c2[ord(s2[i]) - 97] += 1 ans = "Yes" for j in range(25): if c1[j] < c2[j]: ans = "No" break else: k = c1[j] - c2[j] if k % b: ans = "No" break c1[j] = c2[j] c1[j + 1] += k print(["No", "Yes"][c1 == c2])
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST STRING STRING VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = [int(j) for j in input().split()] s = input() t = input() have = [0] * 27 need = [0] * 27 for i in range(n): have[ord(s[i]) - 97] += 1 for i in range(n): need[ord(t[i]) - 97] += 1 flag = True for i in range(26): if have[i] < need[i] or (have[i] - need[i]) % k != 0: flag = False break have[i + 1] += have[i] - need[i] if flag: print("YES") else: print("NO")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline t = int(input()) for i in range(t): n, k = list(map(int, input().split())) a = input() b = input() one = [0] * 26 two = [0] * 26 for j in range(n): one[ord(a[j]) - ord("a")] += 1 two[ord(b[j]) - ord("a")] += 1 verdict = "Yes" for j in range(26): diff = one[j] - two[j] if diff < 0 or diff % k > 0: verdict = "No" break elif j == 25 and diff > 0: verdict = "No" break if j < 25: one[j + 1] += diff print(verdict)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") for _ in range(int(input())): n, k = map(int, input().split()) a = input() b = input() d1 = [0] * 26 d2 = [0] * 26 for i1, i2 in zip(a, b): d1[ord(i1) - 97] += 1 d2[ord(i2) - 97] += 1 ans = "YES" for i, v in enumerate(d1): if d1[i] != d2[i]: if v < d2[i] or (v - d2[i]) % k or i == 25: ans = "NO" break else: d1[i] = d2[i] d1[i + 1] += v - d2[i] print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) a = input() b = input() l = [0] * 26 z = [0] * 26 x = ord("a") for i in range(n): e = ord(a[i]) l[e - x] += 1 for i in range(n): e = ord(b[i]) z[e - x] += 1 f = 0 for i in range(26): if l[i] < z[i]: f = 1 break else: d = l[i] - z[i] if d % k != 0: f = 1 break if d != 0: l[i + 1] += d if f: print("No") else: print("Yes")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
from sys import stdin inp = lambda: stdin.readline().strip() t = int(inp()) for _ in range(t): n, k = [int(x) for x in inp().split()] a = inp() b = inp() letterA = [0] * 27 letterB = [0] * 27 for i, j in zip(a, b): letterA[ord(i) - 97] += 1 letterB[ord(j) - 97] += 1 for i in range(25, -1, -1): letterA[i] += letterA[i + 1] letterB[i] += letterB[i + 1] if (letterB[i] - letterA[i]) % k: print("No") break elif letterA[i] > letterB[i]: print("No") break else: print("Yes")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
def fun(A, B, n, k): temp = [0] * 26 t = ord("a") for i in range(n): temp[ord(A[i]) - t] -= 1 temp[ord(B[i]) - t] += 1 cu = 0 for i in range(26): if temp[i] == 0: continue else: if abs(temp[i]) % k != 0: print("NO") return if temp[i] > 0: if cu < temp[i]: print("NO") return else: cu -= temp[i] else: cu += abs(temp[i]) print("YES") for _ in range(int(input())): n, k = map(int, input().split()) a = input() b = input() fun(a, b, n, k)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline t = int(input()) for you in range(t): l = input().split() n = int(l[0]) k = int(l[1]) f1 = [(0) for i in range(26)] f2 = [(0) for i in range(26)] a = input() b = input() for i in range(n): f1[ord(a[i]) - 97] += 1 f2[ord(b[i]) - 97] += 1 poss = 1 curr = 0 for i in range(26): curr += f1[i] if curr < f2[i]: poss = 0 break curr -= f2[i] curr = curr - curr % k if poss: print("Yes") else: print("No")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
from sys import stdin input = stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) a = str(input()) b = str(input()) l1 = [0] * 27 l2 = [0] * 27 for i in range(n): l1[int(ord(a[i]) - ord("a"))] += 1 l2[int(ord(b[i]) - ord("a"))] += 1 f = 0 d = 0 for i in range(26): temp = l1[i] - l2[i] if temp < 0: f = 1 break if temp % k != 0: f = 1 break l1[i + 1] += temp if f == 0: print("Yes") else: print("No")
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
from sys import stdin, stdout def alpha_order(c): return ord(c) - ord("a") def solve(): n, k = map(int, input().split()) a = stdin.readline() b = stdin.readline() a_alpha = [0] * 26 b_alpha = [0] * 26 for i in range(n): a_alpha[alpha_order(a[i])] += 1 b_alpha[alpha_order(b[i])] += 1 for c in range(26): if a_alpha[c] < b_alpha[c]: required = (b_alpha[c] - a_alpha[c] + k - 1) // k for i in range(c): possible = min(a_alpha[i] // k, required) a_alpha[i] -= possible * k a_alpha[c] += possible * k required -= possible if required > 0: print("No") return a_alpha[c] -= b_alpha[c] print("Yes") def main(): t = int(input()) for _ in range(t): solve() main()
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys def input(): return sys.stdin.readline()[:-1] for _ in range(int(input())): n, k = map(int, input().split()) A = [0] * 26 B = [0] * 26 for a in input(): A[ord(a) - 97] += 1 for b in input(): B[ord(b) - 97] += 1 ans = "Yes" for i in range(25): if A[i] >= B[i] and (A[i] - B[i]) % k == 0: A[i + 1] += A[i] - B[i] else: ans = "No" break print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline def prog(): for _ in range(int(input())): n, k = map(int, input().split()) strings = [input().strip(), input().strip()] amts = [([0] * 26) for i in range(2)] total = 0 total2 = 0 worked = True for i in range(2): for j in range(n): amts[i][ord(strings[i][j]) - ord("a")] += 1 for j in range(26): total += amts[0][j] total2 += amts[1][j] if total < total2 or (amts[1][j] - amts[0][j]) % k != 0: worked = False print("Yes" if worked else "No") prog()
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s, t = input(), input() c = [0] * 26 for i in range(n): c[ord(s[i]) - 97] += 1 c[ord(t[i]) - 97] -= 1 for j in range(25): d = c[j] if d < 0 or d % k: break c[j] -= d c[j + 1] += d print(["No", "Yes"][all(i == 0 for i in c)])
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST STRING STRING FUNC_CALL VAR VAR NUMBER VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline alp = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ] for _ in range(int(input())): n, k = map(int, input().split()) a = input() b = input() d = dict() for i in alp: d[i] = 0 for i in range(n): d[a[i]] += 1 a = [] for i in alp: a += [i] * d[i] d1 = dict() for i in alp: d1[i] = 0 for i in range(n): d1[b[i]] += 1 b = [] for i in alp: b += [i] * d1[i] con = 0 for i in range(n): if a[i] == b[i]: d[a[i]] -= 1 elif a[i] > b[i]: con = 1 break else: l = 0 itr = i while alp[l] < b[i]: if d[alp[l]] > 0: if d[alp[l]] % k != 0: con = 1 break else: while itr < n and a[itr] == alp[l]: d[a[itr]] -= 1 a[itr] = b[i] d[b[i]] += 1 itr += 1 l += 1 if con == 1: break else: d[a[i]] -= 1 if con == 1: print("no") else: print("yes")
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s1 = input().rstrip() s2 = input().rstrip() c1 = [0] * 26 c2 = [0] * 26 for i in s1: c1[ord(i) - 97] += 1 for i in s2: c2[ord(i) - 97] += 1 ans = "Yes" for i in range(0, 25): if c1[i] == 0 and c2[i] == 0: continue elif c1[i] < c2[i] or (c1[i] - c2[i]) % k > 0: ans = "No" break c1[i + 1] += c1[i] - c2[i] print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() def cal(s): res = [0] * 26 for c in s: res[c - 97] += 1 return res def ok(): cnt = 0 for c1, c2 in zip(cc1, cc2): d = c1 - c2 if d == 0: continue if abs(d) % k: return False cnt += d // k if cnt < 0: return False return True for _ in range(II()): n, k = MI() s = BI() t = BI() cc1 = cal(s) cc2 = cal(t) if ok(): print("Yes") else: print("No")
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) a = input().rstrip() b = input().rstrip() data_a = [(0) for i in range(26)] data_b = [(0) for i in range(26)] for i in range(n): data_a[ord(a[i]) - 97] += 1 data_b[ord(b[i]) - 97] += 1 for i in range(25): if data_a[i] >= data_b[i]: if (data_a[i] - data_b[i]) % k: print("No") break data_a[i + 1] += data_a[i] - data_b[i] else: print("No") break else: print("Yes")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
input = __import__("sys").stdin.readline def solve(a, b, k, n): deca = {} decb = {} for i in a: if i in deca: deca[i] += 1 else: deca[i] = 1 for i in b: if i in decb: decb[i] += 1 else: decb[i] = 1 for j in decb: if j in deca: if deca[j] < decb[j]: decb[j] -= deca[j] deca[j] = 0 else: deca[j] -= decb[j] decb[j] = 0 for j in decb: if decb[j] % k != 0: return "NO" for j in deca: if deca[j] % k != 0: return "NO" q = "" p = "" for i in decb: if decb[i] != 0: q += i for i in deca: if deca[i] != 0: p += i p = sorted(p) q = sorted(q) i = 0 j = 0 while i < len(p) and j < len(q): if p[i] < q[j]: if deca[p[i]] < decb[q[j]]: decb[q[j]] -= deca[p[i]] i += 1 elif deca[p[i]] == decb[q[j]]: i += 1 j += 1 else: deca[p[i]] -= decb[q[j]] j += 1 else: return "NO" return "YES" for _ in range(int(input())): n, k = map(int, input().split()) a = list(input()) b = list(input()) print(solve(a, b, k, n))
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN STRING FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR NUMBER VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = lambda: sys.stdin.readline().strip() t = int(input()) while t: t -= 1 n, k = map(int, input().split()) a = input() b = input() freq = [0] * 26 freq1 = [0] * 26 for i in a: freq[ord(i) - ord("a")] += 1 for i in b: freq1[ord(i) - ord("a")] += 1 ok = True add = 0 for i in range(26): if freq[i] == freq1[i]: continue if freq[i] > freq1[i]: extra = freq[i] - freq1[i] if extra % k == 0: add += extra // k else: ok = False break else: req = freq1[i] - freq[i] if req % k == 0: temp = req // k if temp <= add: add -= temp else: ok = False break else: ok = False break if ok and add == 0: print("Yes") else: print("No")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) a = input() b = input() l1 = [0] * 26 l2 = [0] * 26 for i in range(n): l1[ord(a[i]) - 97] += 1 l2[ord(b[i]) - 97] += 1 f = False for i in range(25): z = l1[i] - l2[i] if z < 0 or z % k: f = True break l1[i] = l2[i] l1[i + 1] += z print(["No", "Yes"][l1 == l2])
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST STRING STRING VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
def solve(a, b, k, n): a.sort() b.sort() indexA = 0 indexB = 0 curr = 0 while indexA < n and indexB < n: if a[indexA] > b[indexB] and curr == 0: return "No" elif a[indexA] < b[indexB]: if indexA + k - 1 < n and a[indexA + k - 1] == a[indexA]: curr += k indexA += k else: return "No" elif a[indexA] > b[indexB] and curr > 0: if indexB + k - 1 < n and b[indexB + k - 1] == b[indexB]: curr -= k indexB += k else: return "No" else: indexA += 1 indexB += 1 while indexB < n: if indexB + k - 1 < n and b[indexB + k - 1] == b[indexB]: curr -= k indexB += k else: return "No" return "Yes" for _ in range(int(input())): n, k = list(map(int, input().split())) a = list(input()) b = list(input()) print(solve(a, b, k, n))
FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN STRING IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN STRING IF VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN STRING VAR NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = lambda: sys.stdin.readline().rstrip() t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = input() b = input() ca = [(0) for i in range(26)] cb = [(0) for i in range(26)] for i in range(n): ca[ord(a[i]) - 97] += 1 cb[ord(b[i]) - 97] += 1 c = 0 ok = True for i in range(26): x = ca[i] - cb[i] if x % k: ok = False break c += x if c < 0: ok = False break if ok: print("yes") else: print("no")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(input()) b = list(input()) a_cnt, b_cnt = [0] * 27, [0] * 27 ans = "YES" for i in range(n): a_cnt[ord(a[i]) - 97] += 1 b_cnt[ord(b[i]) - 97] += 1 for i in range(26): if a_cnt[i] < b_cnt[i] or (a_cnt[i] - b_cnt[i]) % k != 0: ans = "NO" break a_cnt[i] -= b_cnt[i] a_cnt[i + 1] += a_cnt[i] print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR STRING VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def BI(): return sys.stdin.buffer.readline().rstrip() def cal(s): res = [0] * 26 for c in s: res[c - 97] += 1 return res for _ in range(II()): n, k = MI() a = BI() b = BI() li1 = cal(a) li2 = cal(b) c = 0 f = 0 for x, y in zip(li1, li2): d = x - y if d == 0: continue if abs(d) % k: f = 1 break c += d // k if c < 0: f = 1 break if f: print("NO") else: print("YES")
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
from sys import stdin input = stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = [i for i in input().strip()] b = [i for i in input().strip()] a.sort() b.sort() if a > b: print("NO") else: ok = True count_a = [0] * 26 count_b = [0] * 26 for i in a: count_a[ord(i) - 97] += 1 for i in b: count_b[ord(i) - 97] += 1 for i in range(26): if count_a[i] < count_b[i] or count_a[i] % k != count_b[i] % k: ok = False break if i != 25: count_a[i + 1] += count_a[i] - count_b[i] elif count_a[i] > count_b[i]: ok = False break print("YES" if ok else "NO")
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
from sys import stdin input = stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) a = input().strip() b = input().strip() map1 = [(0) for i in range(26)] map2 = [(0) for j in range(26)] for i in a: map1[ord(i) - ord("a")] += 1 for i in b: map2[ord(i) - ord("a")] += 1 c = 0 for i in range(26): if map1[i] % k != map2[i] % k: print("NO") break c += map1[i] // k if c and c >= map2[i] // k: c -= map2[i] // k continue if map1[i] != map2[i]: print("NO") break else: print("YES")
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def cal(s): res = [0] * 26 for c in s: res[c - 97] += 1 return res for _ in range(II()): n, k = MI() a = BI() b = BI() li1 = cal(a) li2 = cal(b) c = 0 f = 0 for x, y in zip(li1, li2): d = x - y if d == 0: continue if abs(d) % k: f = 1 break c += d // k if c < 0: f = 1 break if f: print("NO") else: print("YES")
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys input = lambda: sys.stdin.readline().rstrip() T = int(input()) for _ in range(T): N, K = map(int, input().split()) A = [(ord(a) - 97) for a in input()] B = [(ord(a) - 97) for a in input()] C1 = [0] * 26 C2 = [0] * 26 for a in A: C1[a] += 1 for a in B: C2[a] += 1 for i in range(26): if C1[i] < C2[i] or (C1[i] - C2[i]) % K: print("No") break if C1[i] != C2[i]: C1[i + 1] += C1[i] - C2[i] else: print("Yes")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for k in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for k in range(c)] for k in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**19 MOD = 10**9 + 7 EPS = 10**-10 for _ in range(INT()): N, K = MAP() S = [(ord(s) - 97) for s in input()] T = [(ord(s) - 97) for s in input()] C1 = [0] * 26 C2 = [0] * 26 for i in range(N): C1[S[i]] += 1 C2[T[i]] += 1 ok = 1 for c in range(26): if C1[c] < C2[c]: ok = 0 break while C1[c] > C2[c]: C1[c] -= K C1[c + 1] += K if C1[c] != C2[c]: ok = 0 break if ok: Yes() else: No()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Ashish has two strings $a$ and $b$, each of length $n$, and an integer $k$. The strings only contain lowercase English letters. He wants to convert string $a$ into string $b$ by performing some (possibly zero) operations on $a$. In one move, he can either choose an index $i$ ($1 \leq i\leq n-1$) and swap $a_i$ and $a_{i+1}$, or choose an index $i$ ($1 \leq i \leq n-k+1$) and if $a_i, a_{i+1}, \ldots, a_{i+k-1}$ are all equal to some character $c$ ($c \neq$ 'z'), replace each one with the next character $(c+1)$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $a$. Help Ashish determine if it is possible to convert string $a$ into $b$ after performing some (possibly zero) operations on it. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $n$ ($2 \leq n \leq 10^6$) and $k$ ($1 \leq k \leq n$). The second line of each test case contains the string $a$ of length $n$ consisting of lowercase English letters. The third line of each test case contains the string $b$ of length $n$ consisting of lowercase English letters. It is guaranteed that the sum of values $n$ among all test cases does not exceed $10^6$. -----Output----- For each test case, print "Yes" if Ashish can convert $a$ into $b$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). -----Examples----- Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Output No Yes No Yes -----Note----- In the first test case it can be shown that it is impossible to convert $a$ into $b$. In the second test case, "abba" $\xrightarrow{\text{inc}}$ "acca" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $\xrightarrow{\text{swap}}$ "aaabab" $\xrightarrow{\text{swap}}$ "aaaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddaabb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddbb" $\xrightarrow{\text{inc}}$ $\ldots$ $\xrightarrow{\text{inc}}$ "ddddcc".
from sys import stdin, stdout t = int(stdin.readline()) for _ in range(t): n, k = [int(x) for x in stdin.readline().split()] a = stdin.readline().strip() b = stdin.readline().strip() a = [ord(char) for char in sorted(a, reverse=True)] b = [ord(char) for char in sorted(b, reverse=True)] answer = -1 while answer == -1: if len(a) == 0: answer = "Yes" elif a[-1] > b[-1]: answer = "No" elif a[-1] == b[-1]: a.pop() b.pop() else: pointer = 0 while pointer < len(a) and a[-1 - pointer] == a[-1]: pointer += 1 if pointer % k != 0: answer = "No" else: for i in range(pointer): a[-1 - i] += 1 stdout.write(answer + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t1 = int(input()) for hi in range(t1): n = int(input()) s = str(input()) p = 0 o = True l = 1 for i in range(0, n, 2): if s[i] != s[i + 1]: p += 1 elif o: c = s[i] o = False elif c != s[i]: l += 1 c = s[i] print(p, l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
for _ in range(int(input())): n = int(input()) a = list(input()) b = a[:] i = 0 count = 0 while i < n: if a[i : i + 2] == ["1", "1"] or a[i : i + 2] == ["0", "0"]: i += 2 else: count += 1 i += 2 if a[0] != a[1]: a[0] = "0" a[1] = "0" d = ["0", "0"] else: d = a[:2] for i in range(2, n, 2): if a[i : i + 2] == ["1", "1"] or a[i : i + 2] == ["0", "0"]: d = a[i : i + 2] else: a[i : i + 2] = d if b[0] != b[1]: b[0] = "1" b[1] = "1" d = ["1", "1"] else: d = b[:2] for i in range(2, n, 2): if b[i : i + 2] == ["1", "1"] or b[i : i + 2] == ["0", "0"]: d = b[i : i + 2] else: b[i : i + 2] = d count1 = 1 count2 = 1 c1 = a[0] c2 = b[0] for i in range(2, n, 2): if a[i] != c1: count1 += 1 c1 = a[i] if b[i] != c2: count2 += 1 c2 = b[i] print(count, min(count1, count2))
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 VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER LIST STRING STRING VAR VAR BIN_OP VAR NUMBER LIST STRING STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR LIST STRING STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER LIST STRING STRING VAR VAR BIN_OP VAR NUMBER LIST STRING STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR LIST STRING STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER LIST STRING STRING VAR VAR BIN_OP VAR NUMBER LIST STRING STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
n = int(input()) while n: n -= 1 dlch = int(input()) ch = input() count = 0 s = "" otv = 1 for i in range(1, dlch, 2): if ch[i] != ch[i - 1]: count += 1 elif ch[i] == "1" and ch[i - 1] == "1": s = s + "1" else: s = s + "0" for i in range(1, len(s)): if s[i] != s[i - 1]: otv += 1 print(count, otv)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
[ print( *__import__("functools").reduce( lambda x, y: [ (x[0] + 1, x[1], x[2]), (x[0], x[1] + (x[2] and y[0] != x[2]), y[0]), ][y[0] == y[1]], zip(*([iter(input())] * 2)), (0, 1, 0), )[:-1] ) for p in (input() for m in range(int(input()))) ]
EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP LIST FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
def solve(): x = int(input()) a = input() position = [] count = 0 counti = 0 l = -1 for i in range(0, x, 2): if a[i : i + 2] == "01" or a[i : i + 2] == "10": counti = counti + 1 else: if l != a[i]: count = count + 1 l = a[i] return counti, max(count, 1) tc = int(input()) for i in range(tc): x = solve() print(x[0], x[1])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
for _ in range(int(input().strip())): n = int(input().strip()) arr = input() ans = 0 t = [] for i in range(0, len(arr), 2): if arr[i] != arr[i + 1]: ans += 1 else: t.append(arr[i]) seg = 1 for i in range(0, len(t) - 1): if t[i] != t[i + 1]: seg += 1 print(ans, seg)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR 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 BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t = int(input()) for _ in range(t): n = int(input()) mas = list(input()) new = [] count = 0 for i in range(1, n, 2): if mas[i] == mas[i - 1]: new.append(int(mas[i])) else: new.append(2) count += 1 line = 1 temp = 2 for i in range(n // 2): if new[i] != 2: if temp == 2: temp = new[i] if temp != new[i]: temp = new[i] line += 1 print(count, line)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
import sys input = sys.stdin.readline def solve(): n = int(input()) s = input().strip() ops = 0 dp = [[float("inf"), float("inf")] for _ in range(n // 2)] if s[0] == s[1]: dp[0][int(s[1])] = 1 else: dp[0][int(s[0])] = 1 dp[0][int(s[1])] = 1 ops += 1 for i in range(1, n // 2): c1, c2 = s[i * 2], s[i * 2 + 1] if c1 == c2: dp[i][int(c2)] = ( min(dp[i - 1][0], dp[i - 1][1] + 1) if c2 == "0" else min(dp[i - 1][0] + 1, dp[i - 1][1]) ) else: dp[i][0] = min(dp[i - 1][0], dp[i - 1][1] + 1) dp[i][1] = min(dp[i - 1][0] + 1, dp[i - 1][1]) ops += 1 return ops, min(dp[-1]) for _ in range(int(input())): print(*solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
T = int(input()) for _ in range(0, T): n = int(input()) s = input() x1 = -1 x = 0 y = 0 f = 0 for i in range(0, n // 2): j1 = i * 2 j2 = i * 2 + 1 if s[j1] == s[j2] and x1 != int(s[j1]): y += 1 x1 = int(s[j1]) elif s[j1] != s[j2]: x += 1 f = 1 if y == 0: y = 1 print(x, y, end="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
def countsegs(s): cnt = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: cnt += 1 return cnt tc = int(input()) for case in range(tc): n = int(input()) s = list(input()) moves = 0 for i in range(0, n, 2): if s[i] != s[i + 1]: moves += 1 prevs = ["0", "1"] mn = countsegs(s) for pre in prevs: x = s[:] for i in range(0, n, 2): if x[i] == x[i + 1]: pre = x[i] else: x[i] = pre x[i + 1] = pre mn = min(mn, countsegs(x)) print(moves, mn)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST STRING STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t = int(input()) out = "" for _ in range(t): n = int(input()) string = input() ans = 0 ans2 = 1 last = "" for i in range(0, len(string), 2): if string[i] != string[i + 1]: ans += 1 else: if last != "" and string[i] != last: ans2 += 1 last = string[i] out += str(ans) + " " + str(ans2) + "\n" print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR STRING VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
def solve(n, s): op = 0 state = [(1, 1)] for i in range(0, len(s), 2): j = i + 1 if s[i] == s[j]: v = int(s[i]) x = min(state[-1][1 - v] + 1, state[-1][v]) state.append((x, float("inf")) if v == 0 else (float("inf"), x)) else: op += 1 state.append( ( min(state[-1][0], state[-1][1] + 1), min(state[-1][0] + 1, state[-1][1]), ) ) print(f"{op} {min(state[-1][0], state[-1][1])}") t = int(input()) 10000 while t: n = int(input()) s = input() solve(n, s) t -= 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
for _ in range(int(input())): n = int(input()) s = input() i = 0 count = 0 while i < n - 1: if s[i] != s[i + 1]: count += 1 i += 2 i = 0 prev = None count2 = 1 while i < n - 1: if s[i] == s[i + 1]: if prev is not None: if prev != s[i]: count2 += 1 prev = s[i] i += 2 print(count, count2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
import sys def input(): return sys.stdin.readline().rstrip() def main(): t = int(input()) for tc in range(t): n = int(input()) string = input() opr_cnt = 0 sub_cnt = 0 cur_num = "NULL" for i in range(0, n, 2): if string[i] == string[i + 1]: if string[i] != cur_num: sub_cnt += 1 cur_num = string[i] else: opr_cnt += 1 sub_cnt = 1 if sub_cnt == 0 else sub_cnt print(opr_cnt, sub_cnt) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
import sys input = sys.stdin.readline def solve(): n = int(input()) s = input() ans1 = 0 ans2 = 0 ans = "" last = "!" for i in range(0, n, 2): if s[i] != s[i + 1]: ans1 += 1 elif s[i] != last: ans2 += 1 last = s[i] if ans2 == 0: ans2 += 1 print(ans1, ans2) return for _ in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t = int(input()) for _ in range(t): n = int(input()) s = input() num, cnt = 0, 0 tmp = -1 for i in range(0, n, 2): if s[i] != s[i + 1]: num += 1 elif s[i] != tmp: cnt += 1 tmp = s[i] print(num, max(1, cnt))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t = int(input()) for _ in range(t): n = int(input()) s = list(input()) l = "1" ans = 0 for i in range(0, n, 2): if s[i] == s[i + 1]: l = s[i + 1] break for i in range(0, len(s), 2): if s[i] == s[i + 1]: l = s[i + 1] else: ans += 1 s[i] = l s[i + 1] = l ans1 = 0 l = "b" for i in range(n): if s[i] != l: ans1 += 1 l = s[i] print(ans, ans1)
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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
for i in range(int(input())): n = int(input()) s = input() counter_change = 0 counter_segment = 0 p = "p" for k in range(0, n, 2): if s[k] != s[k + 1]: counter_change += 1 else: if p != s[k]: counter_segment += 1 p = s[k] if counter_segment == 0: counter_segment = 1 print(counter_change, counter_segment)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
for zzzzz in range(int(input())): n = int(input()) a = input() count = 0 isprev = "-1" count2 = 0 for i in range(0, n, 2): f = a[i] s = a[i + 1] if f != s: count = count + 1 if f == s: if isprev == "-1": if f == "0": isprev = "0" else: isprev = "1" count2 = count2 + 1 elif isprev != f: count2 = count2 + 1 isprev = f if count2 == 0: count2 = 1 print(count, count2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
for t in range(int(input())): n = int(input()) a = [int(y) for y in input()] p = -1 ans = 0 seg = 0 for i in range(0, n, 2): if a[i] ^ a[i + 1]: ans += 1 else: seg += p != a[i] p = a[i] print(ans, max(1, seg))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t = int(input()) for _ in range(t): n = int(input()) st = input() arr = [] count = 1 for i in range(n - 1): if st[i] != st[i + 1]: arr.append(count) count = 1 else: count += 1 ans = 0 for i in range(len(arr)): if arr[i] % 2 != 0: ans += 1 if i + 1 < len(arr): arr[i + 1] -= 1 ct_sb = 1 prev = -1 for i in range(0, n, 2): if st[i] == st[i + 1]: if prev == -1: prev = i elif st[prev] != st[i]: prev = i ct_sb += 1 print(ans, ct_sb)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
def isGood(s): if s[0] == s[1]: return 1 return 0 for _ in range(int(input())): n = int(input()) s = input() ans = 0 kekw = [] kekw2 = [] for i in range(0, n, 2): if s[i : i + 2] == "10" or s[i : i + 2] == "01": kekw.append(i) ans += 1 else: kekw2.append(i) if len(kekw) == n // 2: print(ans, 1) else: temp = 1 i = 0 while i < len(kekw2) - 1: if s[kekw2[i] : kekw2[i] + 2] == s[kekw2[i + 1] : kekw2[i + 1] + 2]: pass else: temp += 1 i += 1 print(ans, temp)
FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN 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 ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
for ii in range(int(input())): n = int(input()) s = input() ans = 0 for i in range(0, n - 1, 2): if s[i] != s[i + 1]: ans += 1 seg = 0 last = 2 for i in range(0, n - 1, 2): curr = 2 if s[i] == s[i + 1] and s[i] == "0": curr = 0 if s[i] == s[i + 1] and s[i] == "1": curr = 1 if last == 0 and curr == 1: seg += 1 last = 1 if last == 1 and curr == 0: seg += 1 last = 0 if last == 2: last = curr print(ans, seg + 1)
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 NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t = int(input()) for i in range(0, t): count = 0 n = int(input()) s = input() j = 0 f = 0 k = 5 for i in range(0, n, 2): if s[i] != s[i + 1]: count += 1 elif f == 0: f = 1 k = s[i] elif s[i] != k: j += 1 k = s[i] print(count, end=" ") print(j + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t = int(input()) for vvod in range(t): n = int(input()) str = input() s = list(str) ans = 0 pok = 0 if n == 2: if s[0] == s[1]: print("0 1") else: print("1 1") continue d = dict() c = "p" ans2 = 0 for i in range(0, n - 1, 2): if s[i] == s[i + 1]: if c != s[i]: ans2 += 1 c = s[i] else: ans += 1 if c == "p": ans2 = 1 print(ans, ans2)
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 NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t = int(input()) for _ in range(t): n = int(input()) s = input() l = 0 k = 0 a = [] for i in range(0, n, 2): if s[i] == s[i + 1]: a.append(int(s[i])) else: a.append(-1) l += 1 x = [] for i in range(len(a)): if a[i] == -1: continue elif a[i] == 0: if not x: x.append(a[i]) elif x[-1] != a[i]: x.append(a[i]) k += 3 elif not x: x.append(a[i]) k += 3 elif x[-1] != a[i]: x.append(a[i]) k += 3 if x: k += 2 if not x: x.append(1) print(l, len(x))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
for _ in range(int(input())): n, S = int(input()), input() DP_, DP0, DP1, p0, p1 = [0], [1], [1], "0", "1" for i in range(0, n, 2): DP_.append(0) DP0.append(0) DP1.append(0) if S[i] == S[i + 1]: DP0[-1] = DP0[-2] + (0 if p0 == S[i] else 1) DP1[-1] = DP1[-2] + (0 if p1 == S[i] else 1) DP_[-1], p0, p1 = DP_[-2], S[i], S[i] else: DP_[-1], DP0[-1], DP1[-1] = 1 + DP_[-2], DP0[-2], DP1[-2] pass print(DP_[-1], min(DP0[-1], DP1[-1])) pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR LIST NUMBER LIST NUMBER LIST NUMBER STRING STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $s$ of length $n$, consisting only of zeros and ones, $n$ is even. Now Tokitsukaze divides $s$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $s$ is considered good if the lengths of all subsegments are even. For example, if $s$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $2$, $2$, $4$ respectively, which are all even numbers, so "11001111" is good. Another example, if $s$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $3$, $2$, $2$, $3$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $s$ good by changing the values of some positions in $s$. Specifically, she can perform the operation any number of times: change the value of $s_i$ to '0' or '1' ($1 \leq i \leq n$). Can you tell her the minimum number of operations to make $s$ good? Meanwhile, she also wants to know the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Input----- The first contains a single positive integer $t$ ($1 \leq t \leq 10000$) β€” the number of test cases. For each test case, the first line contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of $s$, it is guaranteed that $n$ is even. The second line contains a binary string $s$ of length $n$, consisting only of zeros and ones. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single line with two integers β€” the minimum number of operations to make $s$ good, and the minimum number of subsegments that $s$ can be divided into among all solutions with the minimum number of operations. -----Examples----- Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 -----Note----- In the first test case, one of the ways to make $s$ good is the following. Change $s_3$, $s_6$ and $s_7$ to '0', after that $s$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $2$ and $8$ respectively, the number of subsegments of it is $2$. There are other ways to operate $3$ times to make $s$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $2$, $4$, $4$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $2$. In the second, third and fourth test cases, $s$ is good initially, so no operation is required.
t = int(input()) for i in range(t): n = int(input()) a = input() count = 0 count1 = 0 flag = 0 mark = "2" for k in range(n // 2): if a[2 * k] != a[2 * k + 1]: count += 1 elif a[2 * k] != mark: mark = a[2 * k] count1 += 1 if count1 == 0: count1 += 1 print(count, count1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an undirected tree consisting of $n$ vertices. An undirected tree is a connected undirected graph with $n - 1$ edges. Your task is to add the minimum number of edges in such a way that the length of the shortest path from the vertex $1$ to any other vertex is at most $2$. Note that you are not allowed to add loops and multiple edges. -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of vertices in the tree. The following $n - 1$ lines contain edges: edge $i$ is given as a pair of vertices $u_i, v_i$ ($1 \le u_i, v_i \le n$). It is guaranteed that the given edges form a tree. It is guaranteed that there are no loops and multiple edges in the given edges. -----Output----- Print a single integer β€” the minimum number of edges you have to add in order to make the shortest distance from the vertex $1$ to any other vertex at most $2$. Note that you are not allowed to add loops and multiple edges. -----Examples----- Input 7 1 2 2 3 2 4 4 5 4 6 5 7 Output 2 Input 7 1 2 1 3 2 4 2 5 3 6 1 7 Output 0 Input 7 1 2 2 3 3 4 3 5 3 6 3 7 Output 1 -----Note----- The tree corresponding to the first example: [Image] The answer is $2$, some of the possible answers are the following: $[(1, 5), (1, 6)]$, $[(1, 4), (1, 7)]$, $[(1, 6), (1, 7)]$. The tree corresponding to the second example: [Image] The answer is $0$. The tree corresponding to the third example: [Image] The answer is $1$, only one possible way to reach it is to add the edge $(1, 3)$.
import sys def get_new_edges(graph): n = len(graph) far_vertex = [] pi = [None] * n visit = [False] * n visit[0] queue = [[0, 0]] i = 0 while True: if i >= len(queue): break current, d = queue[i] i += 1 visit[current] = True for v in graph[current]: if not visit[v]: u = [v, d + 1] pi[v] = current queue.append(u) if d + 1 > 2: far_vertex.append(u) far_vertex.sort(key=lambda x: -x[1]) pos = [None] * n for i, e in enumerate(far_vertex): pos[e[0]] = i count = 0 for i in range(len(far_vertex)): if not far_vertex[i]: continue vertex, depth = far_vertex[i] father = pi[vertex] count += 1 if pos[father]: far_vertex[pos[father]] = None for u in graph[father]: if pos[u]: far_vertex[pos[u]] = None return count def read_int_line(): return list(map(int, sys.stdin.readline().split())) vertex_count = int(input()) graph = [[] for _ in range(vertex_count)] for i in range(vertex_count - 1): v1, v2 = read_int_line() v1 -= 1 v2 -= 1 graph[v1].append(v2) graph[v2].append(v1) print(get_new_edges(graph))
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NONE FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NONE RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an undirected tree consisting of $n$ vertices. An undirected tree is a connected undirected graph with $n - 1$ edges. Your task is to add the minimum number of edges in such a way that the length of the shortest path from the vertex $1$ to any other vertex is at most $2$. Note that you are not allowed to add loops and multiple edges. -----Input----- The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of vertices in the tree. The following $n - 1$ lines contain edges: edge $i$ is given as a pair of vertices $u_i, v_i$ ($1 \le u_i, v_i \le n$). It is guaranteed that the given edges form a tree. It is guaranteed that there are no loops and multiple edges in the given edges. -----Output----- Print a single integer β€” the minimum number of edges you have to add in order to make the shortest distance from the vertex $1$ to any other vertex at most $2$. Note that you are not allowed to add loops and multiple edges. -----Examples----- Input 7 1 2 2 3 2 4 4 5 4 6 5 7 Output 2 Input 7 1 2 1 3 2 4 2 5 3 6 1 7 Output 0 Input 7 1 2 2 3 3 4 3 5 3 6 3 7 Output 1 -----Note----- The tree corresponding to the first example: [Image] The answer is $2$, some of the possible answers are the following: $[(1, 5), (1, 6)]$, $[(1, 4), (1, 7)]$, $[(1, 6), (1, 7)]$. The tree corresponding to the second example: [Image] The answer is $0$. The tree corresponding to the third example: [Image] The answer is $1$, only one possible way to reach it is to add the edge $(1, 3)$.
from sys import stdin n = int(stdin.readline()) g = dict() for i in range(n - 1): u, v = map(int, stdin.readline().split()) g.setdefault(u - 1, []).append(v - 1) g.setdefault(v - 1, []).append(u - 1) st = [0] rank = [0] * n tree = [0] * n msk = [0] * n rd = dict() while len(st) > 0: top = st.pop() msk[top] = 1 for c in g[top]: if msk[c] == 0: st.append(c) tree[c] = top rank[c] = rank[top] + 1 rd.setdefault(rank[c], []).append(c) max_rank = max(rank) reach = [0] * n build = [0] * n ans = 0 for r in range(max_rank, 2, -1): for node in rd[r]: if reach[node] == 0: reach[node] = 1 reach[tree[node]] = 1 reach[tree[tree[node]]] = 1 build[tree[node]] = 1 print(sum(build))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER EXPR FUNC_CALL FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL FUNC_CALL VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Barbara was late for her math class so as a punishment the teacher made her solve the task on a sheet of paper. Barbara looked at the sheet of paper and only saw n numbers a_1, a_2, …, a_n without any mathematical symbols. The teacher explained to Barbara that she has to place the available symbols between the numbers in a way that would make the resulting expression's value as large as possible. To find out which symbols were available the teacher has given Barbara a string s which contained that information. <image> It's easy to notice that Barbara has to place n - 1 symbols between numbers in total. The expression must start with a number and all symbols must be allowed (i.e. included in s). Note that multiplication takes precedence over addition or subtraction, addition and subtraction have the same priority and performed from left to right. Help Barbara and create the required expression! Input The first line of the input contains a single integer n (1 ≀ n ≀ 10^5) β€” the amount of numbers on the paper. The second line of the input contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 9), where a_i is the i-th element of a. The third line of the input contains the string s (1 ≀ |s| ≀ 3) β€” symbols allowed in the expression. It is guaranteed that the string may only consist of symbols "-", "+" and "*". It is also guaranteed that all symbols in the string are distinct. Output Print n numbers separated by n - 1 symbols β€” a mathematical expression with the greatest result. If there are multiple equally valid results β€” output any one of them. Examples Input 3 2 2 0 +-* Output 2*2-0 Input 4 2 1 1 2 +* Output 2+1+1+2 Note The following answers also fit the first example: "2+2+0", "2+2-0", "2*2+0".
n = int(input()) a = list(map(int, input().split())) s = input() usable = [] if "+" in s: usable.append(0) if "*" in s: usable.append(1) if "-" in s: usable.append(2) usable = tuple(usable) if len(usable) == 1: ans = [s] * (n - 1) ansPr = [] for i in range(n - 1): ansPr.append(str(a[i])) ansPr.append(ans[i]) ansPr.append(str(a[n - 1])) print("".join(ansPr)) elif usable == (0, 1) or usable == (0, 1, 2): ans = ["+"] * (n - 1) curIndex = 0 while curIndex < n: curCpy = curIndex while curIndex < n and a[curIndex] != 0: curIndex += 1 left = curCpy right = curIndex while left < right and a[left] == 1: left += 1 while right > left and a[right - 1] == 1: right -= 1 curCur = left nonOneProd = [] oneLength = [] while curCur < right: curProd = 1 while curCur < right and a[curCur] != 1: if curProd < 2 * (right - left): curProd *= a[curCur] curCur += 1 nonOneProd.append(curProd) if curCur == right: break curOneLength = 0 while curCur < right and a[curCur] == 1: curOneLength += a[curCur] curCur += 1 oneLength.append(curOneLength) curAllProd = 1 index = 0 while curAllProd < 2 * (right - left) and index < len(nonOneProd): curAllProd *= nonOneProd[index] index += 1 if curAllProd >= 2 * (right - left) or len(nonOneProd) == 1: for i in range(left, right - 1): ans[i] = "*" else: maskLen = len(oneLength) bestMask = 0 bestAns = 0 for i in range(1 << maskLen): curAns = 0 curProd = nonOneProd[0] for j in range(maskLen): if i & 1 << j: curProd *= nonOneProd[j + 1] else: curAns += curProd curAns += oneLength[j] curProd = nonOneProd[j + 1] curAns += curProd if curAns > bestAns: bestAns = curAns bestMask = i curOneCount = 0 for i in range(left, right - 1): if a[i] != 1 and a[i + 1] == 1: curOneCount += 1 if a[i] != 1 and a[i + 1] != 1: ans[i] = "*" elif bestMask & 1 << curOneCount - 1: ans[i] = "*" while curIndex < n and a[curIndex] == 0: curIndex += 1 ansPr = [] for i in range(n - 1): ansPr.append(str(a[i])) ansPr.append(ans[i]) ansPr.append(str(a[n - 1])) print("".join(ansPr)) elif usable == (1, 2): ans = ["-"] * (n - 1) firstZero = True for i in range(n - 1): ans[i] = "*" if a[i + 1] == 0 and firstZero: ans[i] = "-" firstZero = False ansPr = [] for i in range(n - 1): ansPr.append(str(a[i])) ansPr.append(ans[i]) ansPr.append(str(a[n - 1])) print("".join(ansPr)) elif usable == (0, 2): ans = ["+"] * (n - 1) ansPr = [] for i in range(n - 1): ansPr.append(str(a[i])) ansPr.append(ans[i]) ansPr.append(str(a[n - 1])) print("".join(ansPr))
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 ASSIGN VAR LIST IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING IF BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR STRING WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Barbara was late for her math class so as a punishment the teacher made her solve the task on a sheet of paper. Barbara looked at the sheet of paper and only saw n numbers a_1, a_2, …, a_n without any mathematical symbols. The teacher explained to Barbara that she has to place the available symbols between the numbers in a way that would make the resulting expression's value as large as possible. To find out which symbols were available the teacher has given Barbara a string s which contained that information. <image> It's easy to notice that Barbara has to place n - 1 symbols between numbers in total. The expression must start with a number and all symbols must be allowed (i.e. included in s). Note that multiplication takes precedence over addition or subtraction, addition and subtraction have the same priority and performed from left to right. Help Barbara and create the required expression! Input The first line of the input contains a single integer n (1 ≀ n ≀ 10^5) β€” the amount of numbers on the paper. The second line of the input contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 9), where a_i is the i-th element of a. The third line of the input contains the string s (1 ≀ |s| ≀ 3) β€” symbols allowed in the expression. It is guaranteed that the string may only consist of symbols "-", "+" and "*". It is also guaranteed that all symbols in the string are distinct. Output Print n numbers separated by n - 1 symbols β€” a mathematical expression with the greatest result. If there are multiple equally valid results β€” output any one of them. Examples Input 3 2 2 0 +-* Output 2*2-0 Input 4 2 1 1 2 +* Output 2+1+1+2 Note The following answers also fit the first example: "2+2+0", "2+2-0", "2*2+0".
n = int(input()) A = list(map(int, input().split())) symbols = list(input()) def solve1(array): j = 0 solution = [] first = True while j < len(array): i = j if not first: solution.append("+") first = False if array[j] == 0: j += 1 while j < len(array) and array[j] == 0: j += 1 j -= 1 solution.extend(list("+".join(["0"] * (j - i + 1)))) else: j += 1 while j < len(array) and array[j] != 0: j += 1 j -= 1 sol = solve11(array[i : j + 1]) solution.extend(sol) j += 1 return "".join(list(map(str, solution))) def solve11(array): count = 0 first_different_from_one_prefix = 0 for i in range(len(array)): if array[i] != 1: break else: first_different_from_one_prefix = i + 1 array = array[first_different_from_one_prefix:] first_part = "+".join(["1"] * first_different_from_one_prefix) first_different_from_one_suffix = len(array) - 1 for i in range(len(array) - 1, -1, -1): if array[i] != 1: break else: first_different_from_one_suffix = i - 1 second_part = "+".join(["1"] * (len(array) - 1 - first_different_from_one_suffix)) array = array[: first_different_from_one_suffix + 1] SOLUTION = [] if len(array) > 0: for i in range(0, len(array)): if array[i] > 2: count += 1 if count >= 20: SOLUTION.extend(list("*".join(list(map(str, array))))) SOLUTION = SOLUTION[::-1] else: DP = [(0) for i in range(len(array))] DP_PARENTS = [(-1) for i in range(len(array))] DP[0] = array[0] for i in range(0, len(array)): if array[i] == 1: DP[i] = DP[i - 1] + 1 DP_PARENTS[i] = i - 1 continue else: PROD = 1 for k in range(i - 1, -2, -1): PROD *= array[k + 1] if k == -1: if DP[i] < PROD: DP[i] = PROD DP_PARENTS[i] = k elif DP[i] < DP[k] + PROD: DP[i] = DP[k] + PROD DP_PARENTS[i] = k current = len(array) - 1 while current != -1: k = DP_PARENTS[current] for i in range(current, k, -1): SOLUTION.append(array[i]) SOLUTION.append("*") SOLUTION.pop() SOLUTION.append("+") current = k SOLUTION.pop() RESULT = "" SOLUTION = list(map(str, SOLUTION[::-1])) parts = [first_part, "".join(SOLUTION), second_part] parts = [part for part in parts if part != ""] return "+".join(parts) def solve2(array): current = 0 solution = [] first_zero = None array = list(map(str, array)) while current < len(array) and array[current] != "0": current += 1 if current == len(array): if array[-1] == "0": return "*".join(array[:-1]) + "-0" else: return "*".join(array) elif current != 0: return "*".join(array[0:current]) + "-" + "*".join(array[current:]) else: return "*".join(array[current:]) MY_SOLUTION = None if len(symbols) == 1: MY_SOLUTION = symbols[0].join(list(map(str, A))) if len(symbols) == 2: if "+" in symbols and "-" in symbols: MY_SOLUTION = "+".join(list(map(str, A))) if "+" in symbols and "*" in symbols: MY_SOLUTION = solve1(A) if "*" in symbols and "-" in symbols: MY_SOLUTION = solve2(A) if len(symbols) == 3: MY_SOLUTION = solve1(A) print(MY_SOLUTION)
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 FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING BIN_OP LIST STRING BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING BIN_OP LIST STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL STRING VAR VAR ASSIGN VAR VAR VAR VAR VAR STRING RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING RETURN BIN_OP FUNC_CALL STRING VAR NUMBER STRING RETURN FUNC_CALL STRING VAR IF VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL STRING VAR NUMBER VAR STRING FUNC_CALL STRING VAR VAR RETURN FUNC_CALL STRING VAR VAR ASSIGN VAR NONE IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF STRING VAR STRING VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF STRING VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Barbara was late for her math class so as a punishment the teacher made her solve the task on a sheet of paper. Barbara looked at the sheet of paper and only saw n numbers a_1, a_2, …, a_n without any mathematical symbols. The teacher explained to Barbara that she has to place the available symbols between the numbers in a way that would make the resulting expression's value as large as possible. To find out which symbols were available the teacher has given Barbara a string s which contained that information. <image> It's easy to notice that Barbara has to place n - 1 symbols between numbers in total. The expression must start with a number and all symbols must be allowed (i.e. included in s). Note that multiplication takes precedence over addition or subtraction, addition and subtraction have the same priority and performed from left to right. Help Barbara and create the required expression! Input The first line of the input contains a single integer n (1 ≀ n ≀ 10^5) β€” the amount of numbers on the paper. The second line of the input contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 9), where a_i is the i-th element of a. The third line of the input contains the string s (1 ≀ |s| ≀ 3) β€” symbols allowed in the expression. It is guaranteed that the string may only consist of symbols "-", "+" and "*". It is also guaranteed that all symbols in the string are distinct. Output Print n numbers separated by n - 1 symbols β€” a mathematical expression with the greatest result. If there are multiple equally valid results β€” output any one of them. Examples Input 3 2 2 0 +-* Output 2*2-0 Input 4 2 1 1 2 +* Output 2+1+1+2 Note The following answers also fit the first example: "2+2+0", "2+2-0", "2*2+0".
from itertools import groupby, product def main(): n = int(input()) ar = [int(t) for t in input().split()] ops = set(input()) if len(ops) == 1: ans = [ops.pop()] * (n - 1) elif ops == {"+", "-"}: ans = ["+"] * (n - 1) elif ops == {"*", "-"}: ans = ["*"] * (n - 1) if 0 in ar: idx = ar.index(0) if idx > 0: ans[idx - 1] = "-" else: ans = ["?"] * (n - 1) def solve(l, r): while l < r: if ar[l] == 1: ans[l] = "+" else: break l += 1 while l < r: if ar[r] == 1: ans[r - 1] = "+" else: break r -= 1 if l == r: return A = ar[l : r + 1] S = max(sum(A), 2 * (r + 1 - l)) P = 1 for x in A: P *= x if P >= S: for j in range(l, r): ans[j] = "*" return nums = [] conns = [] cl = [] i = l for ones, it in groupby(A, key=lambda x: x == 1): if ones: L = len(list(it)) conns.append(L) cl.append(i) i += L else: p = 1 for x in it: p *= x if i < r: ans[i] = "*" i += 1 nums.append(p) best_seq = 0 best_val = sum(A) for seq in range(2 ** len(conns)): i = 0 cur = 0 prod = nums[i] for h in range(len(conns)): op = seq & 1 << h if op: prod *= nums[i + 1] else: cur += prod + conns[i] prod = nums[i + 1] i += 1 cur += prod if cur > best_val: best_val = cur best_seq = seq for h in range(len(conns)): op = best_seq & 1 << h ch = "*" if op else "+" for i in range(cl[h] - 1, cl[h] + conns[h]): ans[i] = ch l = 0 for i in range(n): if ar[i] == 0: if i > 0: ans[i - 1] = "+" if i < n - 1: ans[i] = "+" if l < i - 1: solve(l, i - 1) l = i + 1 if l < n - 1: solve(l, n - 1) res = [None] * (2 * n - 1) res[::2] = ar res[1::2] = ans print(*res, sep="") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR STRING STRING ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER IF VAR STRING STRING ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR RETURN ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING RETURN ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): coins = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1] ans = [] idx = 0 while N and idx < len(coins): if coins[idx] > N: idx += 1 else: N -= coins[idx] ans.append(coins[idx]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): dict = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] ans = [] def help(n, ans): if n == 0: return 0 for i in range(len(dict) - 1, -1, -1): if dict[i] <= n: ans.append(dict[i]) n -= dict[i] break help(n, ans) help(N, ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): coins = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] ans = [] i = 9 while i >= 0: while N >= coins[i]: ans.append(coins[i]) N -= coins[i] i -= 1 if N == 0: return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): currency = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] n = len(currency) target = N i = n - 1 out = [] while i >= 0: curr = currency[i] if target >= curr: target = target - curr out.append(curr) else: i -= 1 return out
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): Coin = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] Cn = 10 ans = [] while N > 0: if Coin[Cn - 1] > N: Cn -= 1 else: ans.append(Coin[Cn - 1]) N -= Coin[Cn - 1] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): a = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] b = 0 c = [] for i in range(9, -1, -1): while b + a[i] <= N: b += a[i] c.append(a[i]) return c
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): l = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] ans = [] for i in range(-1, -11, -1): if N // l[i] != 0: ans += [l[i]] * (N // l[i]) N %= l[i] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP LIST VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, sm): arr, out = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000][::-1], [] for i in arr: if sm // i > 0: out.extend([i] * (sm // i)) sm -= i * (sm // i) return out
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): coins = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] res = [] for i in range(len(coins) - 1, -1, -1): while N >= coins[i]: N -= coins[i] res.append(coins[i]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): brr = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] brr = brr[::-1] ans = [] for i in range(len(brr)): if brr[i] <= N: for j in range(N // brr[i]): ans.append(brr[i]) N = N % brr[i] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): currency = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1] i = 0 ans = [] while i < len(currency): if currency[i] > N: i += 1 elif currency[i] == N: ans.append(currency[i]) return ans elif currency[i] < N: N -= currency[i] ans.append(currency[i]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): coins = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] ans = [] while N > 0: for i in range(9, -1, -1): num = N // coins[i] ans += [coins[i]] * num N = N % coins[i] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST WHILE VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): coins = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1] ans = [] i = 0 while i < len(coins) or N <= 0: while i < len(coins) and coins[i] > N: i += 1 if i >= len(coins): break N -= coins[i] ans.append(coins[i]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, n): arr = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1] ans = [] for i in arr: temp = n // i ans += temp * [i] n -= temp * i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): u = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] u = u[::-1] i = 0 v = [] while N != 0: if N >= u[i]: N = N - u[i] v.append(u[i]) else: i += 1 return v
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): arr = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] curr = len(arr) - 1 ans = [] while N > 0: if arr[curr] <= N: ans.append(arr[curr]) N = N - arr[curr] else: curr = curr - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def minPartition(self, N): currency = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] i = len(currency) - 1 res = [] while i >= 0 and N != 0: if N >= currency[i]: quotient = N // currency[i] res += [currency[i]] * quotient N -= currency[i] * quotient i -= 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR
Given an infinite supply of each denomination of Indian currency { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 } and a target value N. Find the minimum number of coins and/or notes needed to make the change for Rs N. You must return the list containing the value of coins required. Example 1: Input: N = 43 Output: 20 20 2 1 Explaination: Minimum number of coins and notes needed to make 43. Example 2: Input: N = 1000 Output: 500 500 Explaination: minimum possible notes is 2 notes of 500. Your Task: You do not need to read input or print anything. Your task is to complete the function minPartition() which takes the value N as input parameter and returns a list of integers in decreasing order. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≀ N ≀ 10^{6}
currencies = [1, 2, 5, 10, 20, 50, 100, 200, 500, 2000] num_currencies = len(currencies) class Solution: def minPartition(self, N): i = num_currencies - 1 taken = [] while i >= 0 and N > 0: while currencies[i] <= N: N -= currencies[i] taken.append(currencies[i]) i -= 1 return taken
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR