description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): provinces = 0 visited = [False] * V res = [] for node in range(V): if not visited[node]: provinces += 1 queue = [node] visited[node] = True while queue: node = queue.pop(0) for i in range(V): if adj[node][i] == 1 and not visited[i]: queue.append(i) visited[i] = True return provinces
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): n = len(adj) visited = [(0) for _ in range(n)] def dfs(i): visited[i] = 1 for city in range(n): if not visited[city] and adj[i][city]: dfs(city) return count = 0 for i in range(0, n): if not visited[i]: dfs(i) count += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): visited = [0] * V cnt = 0 for i in range(V): if not visited[i]: cnt += 1 self.dfs(i, visited, adj, cnt) return cnt def dfs(self, node, visited, adj, cnt): visited[node] = 1 for i in range(len(adj)): if adj[node][i] == 1 and not visited[i]: self.dfs(i, visited, adj, cnt)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): def bfs(V, adjLS): final = [] queue = [] queue.append(V) while len(queue) > 0: element = queue.pop(0) final.append(element) for i in adjLs[element]: if i == -1: continue if visited[i] == False: queue.append(i) visited[i] = True visited = [False] * V adjLs = [[-1] for i in range(V)] c = 0 for i in range(0, V): for j in range(0, V): if adj[i][j] == 1 and i != j: adjLs[i].append(j) for i in range(0, V): if visited[i] == False: visited[i] = True c = c + 1 bfs(i, adjLs) return c
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
def make_adj(mat, adj): for i in range(len(mat)): for j in range(len(mat[0])): if mat[i][j] == 1 and i != j: adj[i].append(j) return adj def dfs(adj, s, visited): visited[s] = True for u in adj[s]: if visited[u] == False: dfs(adj, u, visited) def dfs_connected(adj): visited = [False] * len(adj) count = 0 for u in range(len(adj)): if visited[u] == False: count += 1 dfs(adj, u, visited) return count class Solution: def numProvinces(self, adj, V): adj_edges = [[] for i in range(V)] adj = make_adj(adj, adj_edges) return dfs_connected(adj)
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): visited = [(0) for i in range(V)] ans = 0 def traverse(i): if visited[i] == 0: visited[i] = 1 for j in range(V): if adj[i][j] == 1 and i != j and visited[j] == 0: traverse(j) for i in range(V): if visited[i] == 0: ans += 1 visited[i] = 1 for j in range(V): if adj[i][j] == 1 and i != j and visited[j] == 0: traverse(j) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): if V == 0: return 0 visited = [(0) for i in range(V)] count = 0 for i in range(V): if visited[i] == 0: count = count + 1 self.dfs(i, -1, visited, adj, V) return count def dfs(self, node, prev, visited, adj, V): visited[node] = 1 for i in range(V): if visited[i] == 0 and i != prev and adj[node][i]: self.dfs(i, node, visited, adj, V)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): def bfs(adjlst, node, visited, q): visited[node] = 1 q.append(node) while q: f = q[0] q.pop(0) for i in adjlst[f]: if visited[i] != 1: visited[i] = 1 q.append(i) visited = [0] * V q = [] c = 0 adjlst = [[] for i in range(V)] for i in range(V): for j in range(V): if adj[i][j] == 1 and i != j: adjlst[i].append(j) adjlst[j].append(i) for i in range(V): if visited[i] != 1: c += 1 bfs(adjlst, i, visited, q) return c
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): class UnionFind: def __init__(self, size): self.root = {i: i for i in range(size)} self.rank = [0] * size self.size = size def find(self, member): root = member while root != self.root[root]: root = self.root[root] while member != root: parent = self.root[member] self.root[member] = root member = parent return root def union(self, x, y): rootX = self.find(x) rootY = self.find(y) if rootX != rootY: if self.rank[rootX] > self.rank[rootY]: self.root[rootY] = rootX elif self.rank[rootX] < self.rank[rootY]: self.root[rootX] = rootY else: self.root[rootY] = rootX self.rank[rootX] += 1 def connected(self, x, y): return self.root[x] == self.root[y] check = UnionFind(V) for i in range(V): for j in range(V): if adj[i][j]: check.union(i, j) parent = 0 for i in range(V): if check.root[i] == i: parent += 1 return parent
CLASS_DEF FUNC_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
def dfs(adj, i, vis): vis.add(i) for j in adj[i]: if j not in vis: dfs(adj, j, vis) class Solution: def numProvinces(self, adj, V): vis = set() ans = 0 mp = {} for i in range(V): mp[i] = [] for i in range(V): for j in range(V): if adj[i][j] == 1: mp[i].append(j) mp[j].append(i) for i in range(V): if i not in vis: ans += 1 dfs(mp, i, vis) return ans
FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def findparent(self, node, parent): if parent[node] == node: return node parent[node] = self.findparent(parent[node], parent) return parent[node] def unite(self, a, b, parent): m = self.findparent(a, parent) n = self.findparent(b, parent) parent[max(m, n)] = min(m, n) def numProvinces(self, adj, V): queue = [] parent = [i for i in range(V)] for i in range(V): for j in range(V): if adj[i][j] == 1 and i != j: queue.append([i, j]) while len(queue): a, b = queue.pop() if self.findparent(a, parent) == self.findparent(b, parent): continue self.unite(a, b, parent) count = 0 for i in range(V): if i == parent[i]: count += 1 return count
CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): def dfs(adjlst, node, visited): visited[node] = 1 for i in adjlst[node]: if visited[i] != 1: dfs(adjlst, i, visited) visited = [0] * V q = [] c = 0 adjlst = [[] for i in range(V)] for i in range(V): for j in range(V): if adj[i][j] == 1 and i != j: adjlst[i].append(j) adjlst[j].append(i) for i in range(V): if visited[i] != 1: c += 1 dfs(adjlst, i, visited) return c
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): def dfs(adj, i): adj[i][i] = 0 for j in range(len(adj)): if adj[i][j] == 1: adj[i][j] = 0 if adj[j][j] == 1: dfs(adj, j) ans = 0 for i in range(V): if adj[i][i] == 0: continue ans += 1 dfs(adj, i) return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def numProvinces(self, adj, V): n = len(adj) visited = [0] * n adjj = [[] for i in range(V)] for i in range(0, n): for j in range(0, n): if adj[i][j] == 1 and i != j: adjj[i].append(j) adjj[j].append(i) count = 0 for i in range(0, n): if visited[i] == 0: count += 1 self.dfs(i, visited, adjj) return count def dfs(self, node, visited, adjj): visited[node] = 1 for neigh in adjj[node]: if visited[neigh] == 0: self.dfs(neigh, visited, adjj)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to find the number of provinces. Note: A province is a group of directly or indirectly connected cities and no other cities outside of the group. Example 1: Input: [ [1, 0, 1], [0, 1, 0], [1, 0, 1] ] Output: 2 Explanation: The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. Example 2: Input: [ [1, 1], [1, 1] ] Output : 1 Your Task: You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected. Expected Time Complexity: O(V^{2}) Expected Auxiliary Space: O(V) Constraints: 1 ≤ V ≤ 500
class Solution: def bfs(self, adj, V, vertex, visited): queue = [] queue.append(vertex) visited[vertex] = True while queue: node = queue.pop(0) for i in adj[node]: if visited[i] == False: queue.append(i) visited[i] = True def numProvinces(self, adj, V): adjlist = [[] for i in range(V + 1)] for i in range(V): for j in range(V): if adj[i][j] == 1 and i != j: adjlist[i].append(j) adjlist[j].append(i) vis = [(False) for i in range(V)] cnt = 0 for i in range(V): if vis[i] == False: self.bfs(adjlist, V, i, vis) cnt += 1 return cnt
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. -----Input----- There are only two numbers in the input: n and k (5 ≤ n ≤ 10^6, 2 ≤ k ≤ n - 2, gcd(n, k) = 1). -----Output----- You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines. -----Examples----- Input 5 2 Output 2 3 5 8 11 Input 10 3 Output 2 3 4 6 9 12 16 21 26 31 -----Note----- The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]
import sys inf = (1 << 31) - 1 def solve(): n, k = map(int, input().split()) if k > n - k: k = n - k bit = BinaryIndexedTree([0] * n) s = 0 res = 1 ans = [] for i in range(n): t = (s + k) % n if s < t: res += bit.get_sum(t) - bit.get_sum(s + 1) + 1 ans.append(res) else: res += bit.get_sum(n) - bit.get_sum(s + 1) res += bit.get_sum(t) + 1 ans.append(res) bit.add(s, 1) bit.add(t, 1) s = t print(*ans) class BinaryIndexedTree: def __init__(self, a): self.n = len(a) self.bit = [0] * (self.n + 1) for i in range(1, self.n + 1): self.bit[i] += a[i - 1] if i + (i & -i) <= self.n: self.bit[i + (i & -i)] += self.bit[i] def add(self, i, x): i += 1 while i <= self.n: self.bit[i] += x i += i & -i def get_sum(self, r): res = 0 while r > 0: res += self.bit[r] r -= r & -r return res def __starting_point(): solve() __starting_point()
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. -----Input----- There are only two numbers in the input: n and k (5 ≤ n ≤ 10^6, 2 ≤ k ≤ n - 2, gcd(n, k) = 1). -----Output----- You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines. -----Examples----- Input 5 2 Output 2 3 5 8 11 Input 10 3 Output 2 3 4 6 9 12 16 21 26 31 -----Note----- The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]
n, k = map(int, input().split()) if k << 1 > n: k = n - k s = 1 for i in range(n): s += 1 + i * k // n + (i * k + k - 1) // n print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. -----Input----- There are only two numbers in the input: n and k (5 ≤ n ≤ 10^6, 2 ≤ k ≤ n - 2, gcd(n, k) = 1). -----Output----- You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines. -----Examples----- Input 5 2 Output 2 3 5 8 11 Input 10 3 Output 2 3 4 6 9 12 16 21 26 31 -----Note----- The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]
def euclid(a, b): if b == 0: return [1, 0] if b > a: l = euclid(b, a) l.reverse() return l else: quo = a // b rem = a % b l = euclid(b, rem) m = l[0] n = l[1] return [n, m - n * quo] s = input().split() n = int(s[0]) k = int(s[1]) if 2 * k > n: k = n - k a = [(0) for i in range(n)] t = 0 inv = euclid(n, k)[1] % n for i in range(k - 1): t = (i + 1) * inv % n a[t] += 1 a[n - t] += 1 for r in range(2): a[0] += 1 for i in range(1, n): a[i] += a[i - 1] print("\n".join(str(a[i]) for i in range(n)))
FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN LIST VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. -----Input----- There are only two numbers in the input: n and k (5 ≤ n ≤ 10^6, 2 ≤ k ≤ n - 2, gcd(n, k) = 1). -----Output----- You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines. -----Examples----- Input 5 2 Output 2 3 5 8 11 Input 10 3 Output 2 3 4 6 9 12 16 21 26 31 -----Note----- The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]
l = input().split() n = int(l[0]) k = int(l[1]) curr = 0 ans = 1 if k > n // 2: k = n - k for i in range(n): curr += k if i == n - 1: ans = ans + 2 * (curr // n) - 1 elif curr % n < k: ans = ans + 2 * (curr // n) else: ans = ans + 2 * (curr // n) + 1 print(ans, end=" ")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. -----Input----- There are only two numbers in the input: n and k (5 ≤ n ≤ 10^6, 2 ≤ k ≤ n - 2, gcd(n, k) = 1). -----Output----- You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines. -----Examples----- Input 5 2 Output 2 3 5 8 11 Input 10 3 Output 2 3 4 6 9 12 16 21 26 31 -----Note----- The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]
class Fen: def __init__(self, n): self.value = [0] * n def get(self, fro, to): return self.get1(to) - self.get1(fro - 1) def get1(self, to): to = min(to, len(self.value)) result = 0 while to >= 0: result += self.value[to] to = (to & to + 1) - 1 return result def add(self, at, value): while at < len(self.value): self.value[at] += value at = at | at + 1 def solve(): n, k = map(int, input().split()) k = min(k, n - k) count = [0] * n res = [None] * n ans = 1 cur = 0 fen = Fen(n) for iteration in range(n): nxt = (cur + k) % n here = 0 if cur < nxt: here = fen.get(cur + 1, nxt - 1) else: here = fen.get(0, nxt - 1) + fen.get(cur + 1, n - 1) fen.add(cur, 1) fen.add(nxt, 1) cur = nxt ans += here + 1 res[iteration] = ans print(" ".join(map(str, res))) solve()
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. -----Input----- There are only two numbers in the input: n and k (5 ≤ n ≤ 10^6, 2 ≤ k ≤ n - 2, gcd(n, k) = 1). -----Output----- You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines. -----Examples----- Input 5 2 Output 2 3 5 8 11 Input 10 3 Output 2 3 4 6 9 12 16 21 26 31 -----Note----- The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]
n, m = [int(i) for i in input().split()] if m > n // 2: m = n - m ans = [1] count = 0 c = 1 for i in range(n): count += m if count > n: c += 1 count -= n ans.append(ans[-1] + c) c += 1 else: ans.append(ans[-1] + c) ans = ans[1:] print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. -----Input----- There are only two numbers in the input: n and k (5 ≤ n ≤ 10^6, 2 ≤ k ≤ n - 2, gcd(n, k) = 1). -----Output----- You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines. -----Examples----- Input 5 2 Output 2 3 5 8 11 Input 10 3 Output 2 3 4 6 9 12 16 21 26 31 -----Note----- The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]
n, k = list(map(int, input().split())) if n // 2 < k: k = n - k i = 1 count = 1 ans = [1] for _ in range(n): i += k if i > n + 1: count += 1 ans.append(ans[-1] + count) count += 1 i -= n else: ans.append(ans[-1] + count) print(*ans[1:])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. -----Input----- There are only two numbers in the input: n and k (5 ≤ n ≤ 10^6, 2 ≤ k ≤ n - 2, gcd(n, k) = 1). -----Output----- You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines. -----Examples----- Input 5 2 Output 2 3 5 8 11 Input 10 3 Output 2 3 4 6 9 12 16 21 26 31 -----Note----- The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]
n, k = list(map(int, input().strip().split())) if k > n // 2: k = n - k intersection = n * [0] count = 1 done = False i = 0 result = [] for i in range(1, n + 1): nn = i * k // n j = i * k % n if j < k: count += 2 * nn else: count += 2 * nn + 1 result.append(count) result[-1] -= 1 print(" ".join([str(r) for r in result]))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. -----Input----- There are only two numbers in the input: n and k (5 ≤ n ≤ 10^6, 2 ≤ k ≤ n - 2, gcd(n, k) = 1). -----Output----- You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines. -----Examples----- Input 5 2 Output 2 3 5 8 11 Input 10 3 Output 2 3 4 6 9 12 16 21 26 31 -----Note----- The greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. [Image] [Image] [Image] [Image] [Image] [Image]
import sys inf = (1 << 31) - 1 def solve(): n, k = map(int, input().split()) if k > n - k: k = n - k s = 0 res = 1 a = 1 ans = [0] * n for i in range(n): t = (s + k) % n if t < s and t > 0: res += a + 1 a += 2 else: res += a ans[i] = res s = t print(*ans) def __starting_point(): solve() __starting_point()
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): left = 0 top = 0 right = n - 1 bottom = n - 1 res = [[(0) for _ in range(n)] for _ in range(n)] num = 1 while left < right and top < bottom: for i in range(left, right): res[top][i] = num num += 1 for i in range(top, bottom): res[i][right] = num num += 1 for i in range(right, left, -1): res[bottom][i] = num num += 1 for i in range(bottom, top, -1): res[i][left] = num num += 1 left += 1 right -= 1 top += 1 bottom -= 1 if left == right and top == bottom: res[left][top] = num return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): retv = [[(0) for i in range(n)] for j in range(n)] num = n * n i, j, di, dj = 0, 0, 0, 1 for k in range(1, num + 1): retv[i][j] = k if retv[(i + di) % n][(j + dj) % n]: di, dj = dj, -di i += di j += dj return retv
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): arr = [a for a in range(1, n * n + 1)] matrix = [[a for a in range(1, n + 1)] for b in range(1, n + 1)] num = 0 row_s = 0 row_e = n - 1 col_s = 0 col_e = n - 1 while col_e > col_s and row_e > row_s: for i in range(col_s, col_e): matrix[row_s][i] = arr[num] num += 1 for i in range(row_s, row_e): matrix[i][col_e] = arr[num] num += 1 for i in range(col_e, col_s, -1): matrix[row_e][i] = arr[num] num += 1 for i in range(row_e, row_s, -1): matrix[i][col_s] = arr[num] num += 1 row_s += 1 col_s += 1 row_e -= 1 col_e -= 1 if col_s == col_e: for i in range(row_s, row_e + 1): matrix[i][col_e] = arr[num] num += 1 else: for i in range(col_s, col_e + 1): matrix[row_s][i] = arr[num] num += 1 return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): t = 1 temp = [] for i in range(n): temp.append([None] * n) rep = int(n / 2) if n % 2 == 0 else int(n / 2) + 1 k = 1 for i in range(rep): for j in range(i, n - i - 1): temp[i][j] = k k += 1 for j in range(i, n - i - 1): temp[j][n - i - 1] = k k += 1 for j in range(n - i - 1, i, -1): temp[n - i - 1][j] = k k += 1 for j in range(n - i - 1, i, -1): temp[j][i] = k k += 1 if n % 2 == 1: temp[int(n / 2)][int(n / 2)] = n * n return temp
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): matrix = [] if n == 0: return matrix matrix = [([0] * n) for i in range(n)] rowBegin = 0 rowEnd = n - 1 colBegin = 0 colEnd = n - 1 num = 0 while True: for j in range(colBegin, colEnd + 1): num += 1 matrix[rowBegin][j] = num rowBegin += 1 if rowBegin > rowEnd: break for j in range(rowBegin, rowEnd + 1): num += 1 matrix[j][colEnd] = num colEnd -= 1 if colBegin > colEnd: break for j in range(colEnd, colBegin - 1, -1): num += 1 matrix[rowEnd][j] = num rowEnd -= 1 if rowBegin > rowEnd: break for j in range(rowEnd, rowBegin - 1, -1): num += 1 matrix[j][colBegin] = num colBegin += 1 if colBegin > colEnd: break return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): if n == 0: return [] nums = list(range(1, n * n + 1)) matrix = [[nums[-1]]] start = n * n - 1 while start > 0: matrix = matrix[::-1] matrix = [*zip(*matrix)] matrix = [nums[start - len(matrix[0]) : start]] + matrix start = start - len(matrix[0]) return [*map(list, matrix)]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN LIST FUNC_CALL VAR VAR VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): if n < 1: return [] left = 0 right = n - 1 up = 0 bottom = n - 1 self.matrix = [[(0) for i in range(n)] for j in range(n)] count = 1 while left <= right: if left < right: count = self.DrawRow(up, left, right, count) count = self.DrawCol(right, up, bottom, count) count = self.DrawRow(bottom, right, left, count) count = self.DrawCol(left, bottom, up, count) else: count = self.DrawCol(left, up, bottom + 1, count) break up += 1 bottom += -1 left += 1 right += -1 return self.matrix def DrawRow(self, row, start, end, value): add = 1 if start > end: add = -1 for i in range(start, end, add): self.matrix[row][i] = value value += 1 return value def DrawCol(self, col, start, end, value): add = 1 if start > end: add = -1 for i in range(start, end, add): self.matrix[i][col] = value value += 1 return value
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): res = [[(0) for i in range(n)] for j in range(n)] count, rowNum = 1, n rowIndex1, rowIndex2, colIndex1, colIndex2 = 0, n - 1, n - 1, 0 while rowNum >= 1: for i in range(colIndex2, colIndex1 + 1): res[rowIndex1][i] = count count += 1 rowIndex1 += 1 for i in range(rowIndex1, rowIndex2 + 1): res[i][colIndex1] = count count += 1 colIndex1 -= 1 for i in range(colIndex1, colIndex2 - 1, -1): res[rowIndex2][i] = count count += 1 rowIndex2 -= 1 for i in range(rowIndex2, rowIndex1 - 1, -1): res[i][colIndex2] = count count += 1 colIndex2 += 1 rowNum -= 2 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): if n == 0: return [] res = [([0] * n) for _ in range(n)] record = set() rupper, rdown, cleft, cright, val = 0, n - 1, 0, n - 1, 1 while rupper < rdown and cleft < cright: for col in range(cleft, cright): res[rupper][col] = val val += 1 for row in range(rupper, rdown): res[row][cright] = val val += 1 for col in range(cright, cleft, -1): res[rdown][col] = val val += 1 for row in range(rdown, rupper, -1): res[row][cleft] = val val += 1 rupper += 1 rdown -= 1 cleft += 1 cright -= 1 if n % 2 != 0: res[n // 2][n // 2] = val return res
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): matrix = [[(0) for i in range(n)] for i in range(n)] start, end = 0, n - 1 count = 1 for j in range(int(n / 2)): for i in range(start, end + 1): matrix[start][i] = count count += 1 for i in range(start + 1, end + 1): matrix[i][end] = count count += 1 for i in range(end - 1, start - 1, -1): matrix[end][i] = count count += 1 for i in range(end - 1, start, -1): matrix[i][start] = count count += 1 start += 1 end -= 1 if n % 2 != 0: matrix[start][end] = count return matrix
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution: def generateMatrix(self, n): if n == 0: return [] elif n == 1: return [[1]] cnt = 1 ret = [([None] * n) for _ in range(n)] def layer(width, margin): if not width > 0: return nonlocal cnt for i in range(margin, margin + width): ret[margin][i] = cnt cnt += 1 for i in range(margin + 1, margin + width): ret[i][margin + width - 1] = cnt cnt += 1 for i in range(margin + width - 2, margin - 1, -1): ret[margin + width - 1][i] = cnt cnt += 1 for i in range(margin + width - 2, margin, -1): ret[i][margin] = cnt cnt += 1 layer(width - 2, margin + 1) layer(n, 0) return ret
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST IF VAR NUMBER RETURN LIST LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
st = input() cnt = 0 a = [] di = {")": "(", ">": "<", "]": "[", "}": "{"} for i in st: if i in ["(", "<", "[", "{"]: a.append(i) elif a != []: x = a.pop() if x != di[i]: cnt += 1 else: a = [-1] break if a == []: print(cnt) else: print("Impossible")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR IF VAR LIST STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER IF VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
def solve(): S = input() P = {">": "<", "]": "[", ")": "(", "}": "{"} stack = [] n = 0 ans = 0 for i, c in enumerate(S): if c in "<[{(": n += 1 stack.append(c) else: n -= 1 if len(stack) == 0: print("Impossible") return x = stack.pop() if x != P[c]: ans += 1 if n != 0: print("Impossible") return print(ans) def __starting_point(): solve() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
import sys def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def rinput(): return map(int, sys.stdin.readline().strip().split()) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) n = input() a = ["[", "<", "{", "(", "]", ">", "}", ")"] k = 0 s = [] for i in range(len(n)): if a.index(n[i]) > 3: if not s: s = 1 break if s.pop() != a.index(n[i]) - 4: k += 1 else: s += [a.index(n[i])] if s: print("Impossible") else: print(k)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR LIST FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
S = input() N = len(S) lt = {"[", "(", "{", "<"} rt = {"]", ")", "}", ">"} stack = [] res = 0 for i in range(N): if S[i] in lt: stack.append(i) elif not stack: print("Impossible") break else: c = stack.pop() if S[c] + S[i] not in {"[]", "()", "{}", "<>"}: res += 1 else: if not stack: print(res) else: print("Impossible")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING STRING STRING STRING ASSIGN VAR STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR VAR STRING STRING STRING STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() d = ["<", "{", "[", "("] d2 = [">", "}", "]", ")"] a = [] k = 0 sum1 = 0 sum2 = 0 for i in s: if i in d: sum1 += 1 a.append(d.index(i)) else: sum2 += 1 if len(a) == 0: k = "Impossible" break elif a[-1] == d2.index(i): a.pop() elif a[-1] != d2.index(i): a.pop() k += 1 if sum1 != sum2: print("Impossible") else: print(k)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() otc = {"[": "]", "{": "}", "(": ")", "<": ">"} cto = {">": "<", "}": "{", ")": "(", "]": "["} stack = [] res1 = 0 for c in s: if c in otc.keys(): stack.append(c) continue if len(stack) == 0: res1 = -1 break o = stack[len(stack) - 1] stack.pop() if c == otc[o]: continue else: res1 += 1 if res1 == -1 or len(stack) != 0: print("Impossible") else: print(res1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
def getResult(s): if not s: return "Impossible" dic = {"(": ")", "[": "]", "{": "}", "<": ">"} res = 0 stack = [] for x in list(s): if x in dic: stack.append(x) elif len(stack) == 0: return "Impossible" else: cur = stack.pop() if dic[cur] != x: res += 1 return res if len(stack) == 0 else "Impossible" s = input() print(str(getResult(s)))
FUNC_DEF IF VAR RETURN STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
bracket = input() stack = [] bracketType = { "[": (0, "]"), "(": (0, ")"), "<": (0, ">"), "{": (0, "}"), "]": (1,), ")": (1,), ">": (1,), "}": (1,), } res = 0 for b in bracket: if bracketType.get(b) == None: break elif bracketType.get(b)[0] == 0: stack.append(b) elif bracketType.get(b)[0] == 1: if len(stack) == 0: res = "Impossible" break elif b != bracketType[stack.pop()][1]: res += 1 if len(stack): print("Impossible") else: print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING NUMBER STRING NUMBER STRING NUMBER STRING NUMBER STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NONE IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() open_b = ["<", "{", "[", "("] close_b = [">", "}", "]", ")"] stack = [] d = {open_b[i]: close_b[i] for i in range(4)} stack = [] ans = 0 for i in range(len(s)): stack.append(s[i]) if len(stack) >= 2 and stack[-1] in close_b and stack[-2] in open_b: if d[stack[-2]] != stack[-1]: ans = ans + 1 stack.pop() stack.pop() if len(stack): print("Impossible") exit() print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
k, s = 0, [] for q in input(): i = "[{<(]}>)".find(q) if i > 3: if not s: s = 1 break if s.pop() != i - 4: k += 1 else: s += [i] print("Impossible" if s else k)
ASSIGN VAR VAR NUMBER LIST FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR NUMBER IF VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR LIST VAR EXPR FUNC_CALL VAR VAR STRING VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
import sys s = input() stack = [] piar = {"{": "}", "(": ")", "<": ">", "[": "]"} ans = 0 for ch in s: if ch in piar.keys(): stack.append(ch) else: if len(stack) == 0: print("Impossible") sys.exit() if piar[stack.pop()] != ch: ans += 1 if len(stack) != 0: print("Impossible") sys.exit() print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
import sys def fun(): try: str = input() except ValueError: print("Invalid number") return S = {"}": "{", ")": "(", ">": "<", "]": "["} L = [] ans = 0 for c in str: if c in S.values(): L.append(c) pass else: if len(L) > 0: ans += 0 if S[c] == L.pop() else 1 else: ans = -1 break pass if len(L) > 0 or ans < 0: print("Impossible") else: print(ans) pass fun()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
from sys import stdin def main(): s = stdin.readline() stack = [] ok = True ans = 0 close = ">}])" open = "<{[(" for i in range(len(s) - 1): if s[i] in close: if not stack: ok = False break else: i1 = close.index(s[i]) i2 = open.index(stack[-1]) if i1 != i2: ans += 1 stack.pop() else: stack.append(s[i]) if stack or not ok: print("Impossible") else: print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() s1 = [] for i in s: if i == "(": s1.append(1) elif i == "[": s1.append(2) elif i == "{": s1.append(3) elif i == "<": s1.append(4) elif i == ")": s1.append(-1) elif i == "]": s1.append(-2) elif i == "}": s1.append(-3) else: s1.append(-4) m = [] c = 0 for char in s1: if len(m) > 0: if m[-1] > 0: if m[-1] == -char: m.pop() elif char < 0: m.pop() c += 1 else: m.append(char) else: m.append(char) else: m.append(char) if len(m) > 0: print("Impossible") else: print(c)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
def p1(s): stack = [] n = len(s) dic = {")": "(", "}": "{", "]": "[", ">": "<"} cnt = 0 for x in s: if x not in dic: stack.append(x) elif not stack: return "Impossible" elif dic[x] != stack.pop(): cnt += 1 else: pass if not stack: return cnt else: return "Impossible" s = list(input()) print(p1(s))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN STRING IF VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR RETURN VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
try: def ad(expr): stack = [] l = 0 for char in expr: if char in ["(", "{", "[", "<"]: stack.append(char) else: if not stack: return "Impossible" current_char = stack.pop() if current_char == "(": if char != ")" and char == "}" or char == "]" or char == ">": l += 1 if current_char == "{": if char != "}" and char == ")" or char == "]" or char == ">": l += 1 if current_char == "[": if char != "]" and char == "}" or char == ")" or char == ">": l += 1 if current_char == "<": if char != ">" and char == "}" or char == ")" or char == "]": l += 1 if stack: return "Impossible" return l s = input() print(ad(s)) except: pass
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR LIST STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR IF VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR IF VAR STRING IF VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR STRING IF VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR STRING IF VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR STRING IF VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR RETURN STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() cnt = 0 closing = [">", "}", "]", ")"] opening = ["<", "{", "[", "("] oc = 0 cc = 0 for i in s: if i in opening: oc += 1 else: cc += 1 if s[0] in closing or len(s) & 1 or oc != cc: print("Impossible") else: stack = [] for i in s: if i in closing: if len(stack) == 0: print("Impossible") exit(0) if opening[closing.index(i)] == stack[-1]: stack.pop() else: cnt += 1 stack.pop() elif i in opening: stack.append(i) print(cnt)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
def process(S): flip = {"(": ")", "[": "]", "{": "}", "<": ">"} other = {flip[x]: x for x in flip} for x in other: flip[x] = other[x] d = [] answer = 0 for x in S: if x in "([{<": d.append(x) else: if len(d) == 0: return [False, 0] x2 = flip[x] if x2 != d[-1]: answer += 1 d.pop() if len(d) == 0: return [True, answer] else: return [False, 0] S = input() a1, a2 = process(S) if a1: print(a2) else: print("Impossible")
FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST NUMBER VAR RETURN LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
def opposto(s): if s == ")": return "(" elif s == "]": return "[" elif s == "}": return "{" else: return "<" def f(s): n = [0] * 10**6 checker = 0 cont = 0 pos = 0 for c in s: if c in ["(", "[", "{", "<"]: n[checker] = c checker += 1 pos += 1 elif checker == 0: return "Impossible" elif n[checker - 1] == opposto(c): checker -= 1 else: checker -= 1 cont += 1 if pos == len(s) - pos: return cont else: return "Impossible" s = str(input()) print(f(s))
FUNC_DEF IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING IF VAR STRING RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR LIST STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
opening = set(["(", "<", "[", "{"]) closing = set([")", ">", "]", "}"]) def im_noob(s): ans = 0 l = 0 stack = [] for i in s: if i in opening: if i == "[": stack.append("]") elif i == "<": stack.append(">") elif i == "{": stack.append("}") elif i == "(": stack.append(")") l += 1 else: try: if i != stack[l - 1]: ans += 1 stack.pop() l -= 1 else: stack.pop() l -= 1 except: return "Impossible" if l > 0: return "Impossible" return ans s = input() print(im_noob(s))
ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
S = input() stack = [] count = 0 length = len(S) flag = 0 if length % 2: flag = 1 else: for i in range(length): if S[i] == "<" or S[i] == "(" or S[i] == "{" or S[i] == "[": stack.append(S[i]) elif stack != []: if S[i] == ">" and stack.pop() != "<": count += 1 elif S[i] == ")" and stack.pop() != "(": count += 1 elif S[i] == "}" and stack.pop() != "{": count += 1 elif S[i] == "]" and stack.pop() != "[": count += 1 if flag: break else: flag = 1 break if flag != 0 or len(stack) != 0: print("Impossible") else: print(count)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR LIST IF VAR VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() stack = [] res = 0 for c in s: if c in "(<[{": stack.append(c) else: if not stack: print("Impossible") break last = stack.pop() if last + c not in ("[]", "()", "<>", "{}"): res += 1 else: if stack: print("Impossible") else: print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR STRING STRING STRING STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
a = input() b = 0 c = "([{<" d = ")]}>" e = [] f = False for i in a: if i in c: e += [i] else: if not e: print("Impossible") f = True break if d.index(i) != c.index(e[-1]): b += 1 e.pop() if e: print("Impossible") elif not f: print(b)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR LIST VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
st = input() c = 0 b = r = s = l = 0 for i in st: if i in ["[", "<", "{", "("]: c += 1 else: c -= 1 if c < 0: break ans = 0 if c != 0: print("Impossible") else: stack = [] for i in st: if i in ["[", "<", "{", "("]: stack.append(i) elif stack[-1] == "(" and i == ")": stack.pop() continue elif stack[-1] == "<" and i == ">": stack.pop() continue elif stack[-1] == "{" and i == "}": stack.pop() continue elif stack[-1] == "[" and i == "]": stack.pop() continue else: stack.pop() ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR LIST STRING STRING STRING STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR IF VAR LIST STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER STRING VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER STRING VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER STRING VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
from sys import stdin, stdout class SOLVE: def solve(self): R = stdin.readline W = stdout.write s = R()[:-1] brackets, cnt, check = [], 0, 0 for i in range(len(s)): if s[i] in ["(", "<", "{", "["]: brackets.append(s[i]) check += 1 else: if len(brackets) == 0: W("Impossible\n") return 0 if s[i] == ")": if brackets[-1] != "(": cnt += 1 elif s[i] == ">": if brackets[-1] != "<": cnt += 1 elif s[i] == "}": if brackets[-1] != "{": cnt += 1 elif s[i] == "]": if brackets[-1] != "[": cnt += 1 check -= 1 brackets.pop() if check < 0: W("Impossible\n") return 0 if check: W("Impossible\n") return 0 W("%d\n" % cnt) return 0 def main(): s = SOLVE() s.solve() main()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR LIST STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER IF VAR VAR STRING IF VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING IF VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING IF VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING IF VAR NUMBER STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER IF VAR EXPR FUNC_CALL VAR STRING RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() n = len(s) op = {"<": 0, "{": 1, "[": 2, "(": 3} cl = {">": 0, "}": 1, "]": 2, ")": 3} stack = [] flag = True swaps = 0 for i in range(n): if s[i] in op: stack.append(s[i]) elif stack: if op[stack.pop()] != cl[s[i]]: swaps += 1 else: flag = False break if len(stack) != 0: flag = False print(swaps) if flag else print("Impossible")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
stack = [] counter = 0 possible = True for b in input(): if b in "{<([": stack.append(b) else: if not stack: possible = False break popped = stack.pop() if ( b == "}" and popped != "{" or b == ">" and popped != "<" or b == ")" and popped != "(" or b == "]" and popped != "[" ): counter += 1 if possible and not stack: print(counter) else: print("Impossible")
ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
di = {")": "(", ">": "<", "]": "[", "}": "{"} stack = [] s = list(input()) flag = 0 c = 0 for i in s: if i in ["(", "<", "[", "{"]: stack.append(i) elif len(stack) != 0: x = stack.pop() if x != di[i]: c += 1 else: stack = [0] break if len(stack) == 0: print(c) else: print("Impossible")
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR LIST STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() def fun(): t = ["[", "<", "{", "("] t1 = ["]", ">", "}", ")"] res = [] summ = 0 for i in range(len(s)): try: if s[i] in t: res.append(s[i]) elif s[i] in t1: d = len(res) - 1 if t.index(res[d]) != t1.index(s[i]): summ += 1 res.pop(d) except: print("Impossible") break else: if len(res) == 0: print(summ) else: print("Impossible") fun()
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = list(input()) d = {"(": ")", "[": "]", "<": ">", "{": "}"} stack = [] ans = 0 c = 0 for i in s: if i in d.keys(): stack.append(i) c += 1 else: c -= 1 if c < 0: print("Impossible") exit() b = stack.pop() if i != d[b]: ans += 1 if c == 0: print(ans) else: print("Impossible")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
opening = set(["(", "<", "[", "{"]) closing = set([")", ">", "]", "}"]) def im_noob(s): o = c = boom = 0 for i in s: if i in opening: o += 1 boom += 1 else: c += 1 boom -= 1 if boom < 0: return "Impossible" if o != c: return "Impossible" if boom != 0: return "Impossible" ans = 0 stack = [] for i in s: if i in opening: if i == "[": stack.append("]") elif i == "<": stack.append(">") elif i == "{": stack.append("}") elif i == "(": stack.append(")") elif i != stack[len(stack) - 1]: ans += 1 stack.pop() else: stack.pop() return ans s = input() print(im_noob(s))
ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR VAR RETURN STRING IF VAR NUMBER RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() open = "({<[" close = ")}>]" clopen = {"]": "[", "}": "{", ">": "<", ")": "("} ir2 = 0 ir = 0 if len(s) % 2 == 1: print("Impossible") return else: l = [] for i in range(len(s)): if s[i] in open: ir += 1 l.append(s[i]) else: ir -= 1 if ir == -1: print("Impossible") return p = l.pop() if clopen[s[i]] != p: ir2 += 1 if ir != 0: print("Impossible") return else: print(ir2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
seq = input() a = [] fl = True z = 0 for i in seq: if i == "<" or i == "[" or i == "{" or i == "(": a.append(i) elif i == ">": if len(a) and a[-1] == "<": a.pop() elif len(a) and a[-1] != "<": a.pop() z += 1 else: fl = False break elif i == ")": if len(a) and a[-1] == "(": a.pop() elif len(a) and a[-1] != "(": a.pop() z += 1 else: fl = False break elif i == "]": if len(a) and a[-1] == "[": a.pop() elif len(a) and a[-1] != "[": a.pop() z += 1 else: fl = False break elif i == "}": if len(a) and a[-1] == "{": a.pop() elif len(a) and a[-1] != "{": a.pop() z += 1 else: fl = False break if len(a): fl = False if fl: print(z) else: print("Impossible")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
import sys def rbs(mystr): stack = [] kuohao = {"<": ">", "{": "}", "[": "]", "(": ")"} count = 0 for x in mystr: if x in kuohao: stack.append(x) elif len(stack) == 0: return "Impossible" elif kuohao[stack.pop()] != x: count += 1 else: pass if len(stack) == 0: return count else: return "Impossible" mystr = input() print(rbs(mystr))
IMPORT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING IF VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
import sys s = input() stack = [] ans = 0 brackets = {">": "<", "}": "{", "]": "[", ")": "("} for c in s: if c in ">}])": if not stack: print("Impossible") exit() if stack[-1] != brackets[c]: ans += 1 stack.pop() else: stack.append(c) if len(stack): print("Impossible") else: print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR IF VAR STRING IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
import sys s = input() OPENING = "<", "{", "[", "(" CLOSING = ">", "}", "]", ")" result = 0 stack = [] for c in s: if c in OPENING: stack.append(c) elif stack: last_br = stack.pop() if c != CLOSING[OPENING.index(last_br)]: result += 1 else: print("Impossible") return print("Impossible" if stack else result)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING ASSIGN VAR STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR STRING VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
opening = {"[": "]", "<": ">", "{": "}", "(": ")"} closing = {"]": "[", ">": "<", "}": "{", ")": "("} s = input() stack = [] answ = 0 for c in s: if c in opening: stack.append(c) else: if len(stack) == 0: print("Impossible") return op = stack.pop() if c != opening[op]: answ += 1 if len(stack) != 0: print("Impossible") return print(answ)
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = list(input()) def isopen(c): return c == "<" or c == "{" or c == "[" or c == "(" def match(a, b): return ( a == "<" and b == ">" or a == "{" and b == "}" or a == "[" and b == "]" or a == "(" and b == ")" ) def isRbs(): br = [] if isopen(s[0]) == False: print("Impossible") return False count = 0 for c in s: open = isopen(c) if open == True: br.append(c) elif len(br) > 0 and open == False: b = br.pop() if match(b, c) == False: count += 1 if len(br) == 0: print(count) else: print("Impossible") if len(s) % 2 != 0: print("Impossible") else: isRbs()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR STRING VAR STRING VAR STRING VAR STRING FUNC_DEF RETURN VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
BRACKETS = "{ }, [ ], < >, ( )" openers = dict() closers = dict() for opener, closer in [x.split() for x in BRACKETS.split(",")]: openers[opener] = closer closers[closer] = opener stack = list() impossible = False res = 0 for char in input(): if char in openers: stack.append(char) else: if not stack: impossible = True break elif stack[-1] != closers[char]: res += 1 stack.pop() impossible |= len(stack) > 0 if impossible: print("Impossible") else: print(res)
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
word = input() BRACKETS = [("<", ">"), ("{", "}"), ("[", "]"), ("(", ")")] OPENING = [b[0] for b in BRACKETS] MATCH = dict(BRACKETS) IMPOSSIBLE = "Impossible" def is_opening(w): return w in OPENING def same_kind(wo, wc): return MATCH[wo] == wc def solve(word): temp = [] result = 0 for w in word: if is_opening(w): temp.append(w) else: if len(temp) == 0: return IMPOSSIBLE wo = temp.pop() if not is_opening(wo): return IMPOSSIBLE if not same_kind(wo, w): result += 1 if len(temp) != 0: return IMPOSSIBLE return result print(solve(word))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = str(input()) a = [] v = 0 k = 0 map = {")": "(", "]": "[", "}": "{", ">": "<"} for _ in range(len(s)): if s[_] == "<" or s[_] == "(" or s[_] == "{" or s[_] == "[": a.append(s[_]) elif len(a) == 0: print("Impossible") k = 1 break elif a[-1] == map[s[_]]: a.pop(-1) else: a.pop(-1) v += 1 if k == 0 and len(a) == 0: print(v) elif k == 0: print("Impossible")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
def solution(s): st = [] ans = 0 for i, j in enumerate(s): if j in open: st.append((i, j)) if j in close: if not st: return "Impossible" q, w = st.pop() if close.index(j) != open.index(w): ans += 1 if st: return "Impossible" return ans s = str(input()) open = ["(", "{", "<", "["] close = [")", "}", ">", "]"] print(solution(s))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR RETURN STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() d = [] ans = 0 for f in s: if f in "{[<(": d.append(f) elif f == ")" and d and d[-1] == "(": d.pop() elif f == "}" and d and d[-1] == "{": d.pop() elif f == "]" and d and d[-1] == "[": d.pop() elif f == ">" and d and d[-1] == "<": d.pop() elif not d: print("Impossible") break else: ans += 1 d.pop() else: if not d: print(ans) else: print("Impossible")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF VAR STRING VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF VAR STRING VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF VAR STRING VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
k = input() p = 0 q = [] for i in range(len(k)): if k[i] == "(" or k[i] == "{" or k[i] == "[" or k[i] == "<": q.append(k[i]) else: if len(q) <= 0: print("Impossible") exit(0) m = q.pop() if k[i] == ")" and m == "(": continue elif k[i] == "}" and m == "{": continue elif k[i] == "]" and m == "[": continue elif k[i] == ">" and m == "<": continue else: p += 1 if len(q) != 0: print("Impossible") exit(0) print(p)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR STRING VAR STRING IF VAR VAR STRING VAR STRING IF VAR VAR STRING VAR STRING IF VAR VAR STRING VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() sc = s sc = sc.replace("<", "(").replace("[", "(").replace("{", "(") sc = sc.replace(">", ")").replace("]", ")").replace("}", ")") def chec(ss): c = 0 for i in ss: if i == "(": c += 1 if i == ")": c -= 1 if c < 0: return False return c == 0 if chec(sc): l = [] ans = 0 for i in s: if i in "({[<": l.append(i) else: if i == ")": if l[-1] in "{[<": ans += 1 if i == "}": if l[-1] in "([<": ans += 1 if i == "]": if l[-1] in "{(<": ans += 1 if i == ">": if l[-1] in "{[(": ans += 1 l.pop() print(ans) else: print("Impossible")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING STRING STRING STRING STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING IF VAR NUMBER STRING VAR NUMBER IF VAR STRING IF VAR NUMBER STRING VAR NUMBER IF VAR STRING IF VAR NUMBER STRING VAR NUMBER IF VAR STRING IF VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
def stacks(brackets): opening = ["[", "{", "(", "<"] closing = ["}", "{", "(", "<"] stack = [] changes = 0 for b in brackets: if b in opening: stack.append(b) else: if len(stack) == 0: return -1 last_brackets = stack.pop() if b == "]": if last_brackets != "[": changes += 1 elif b == "}": if last_brackets != "{": changes += 1 elif b == ")": if last_brackets != "(": changes += 1 elif b == ">": if last_brackets != "<": changes += 1 else: return -1 if stack: return -1 else: return changes _in = input() brackets = [] for b in _in: brackets.append(str(b)) v = stacks(brackets) if v == -1: print("Impossible") else: print(v)
FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR STRING IF VAR STRING VAR NUMBER IF VAR STRING IF VAR STRING VAR NUMBER IF VAR STRING IF VAR STRING VAR NUMBER IF VAR STRING IF VAR STRING VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s2 = input() dic = {"{": 1, "}": -1, "<": 2, ">": -2, "[": 3, "]": -3, "(": 4, ")": -4} stack = [] count = 0 n = 0 for x in s2: if dic[x] < 0: if n == 0: print("Impossible") quit() if dic[stack[n - 1]] + dic[x] != 0: count += 1 stack.pop() n = n - 1 else: stack.append(x) n = n + 1 if n != 0: print("Impossible") else: print(count)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
stk = [] ops = 0 f = 1 l = list(input()) for i in l: if i in "{<([": stk.append(i) else: if not stk: f = 0 break c = stk[-1] if c + i in ["[]", "{}", "<>", "()"]: stk.pop() else: ops += 1 stk.pop() if f == 1 and not stk: print(ops) else: print("Impossible")
ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR LIST STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
a = input() s = [] replacements = 0 flag = 0 for i in range(len(a)): if a[i] in "({[<": s.append(a[i]) else: if len(s) == 0: flag = 1 break top = s.pop() if a[i] == ">" and top != "<": replacements += 1 if a[i] == ")" and top != "(": replacements += 1 if a[i] == "]" and top != "[": replacements += 1 if a[i] == "}" and top != "{": replacements += 1 if len(s) > 0 or flag == 1: print("Impossible") else: print(replacements)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR STRING VAR STRING VAR NUMBER IF VAR VAR STRING VAR STRING VAR NUMBER IF VAR VAR STRING VAR STRING VAR NUMBER IF VAR VAR STRING VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
def solve(s: str): stack = [] answer = 0 for c in s: if c in "([{<": stack.append(c) else: if not stack: print("Impossible") return b = stack[-1] if ( b == "(" and c != ")" or b == "[" and c != "]" or b == "{" and c != "}" or b == "<" and c != ">" ): answer += 1 stack.pop() print("Impossible" if stack else answer) s = input() solve(s)
FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR NUMBER IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() flag = 0 stack = [] op = ["<", "{", "[", "("] ans = 0 for i in range(len(s)): if s[i] in op: flag += 1 else: flag -= 1 if flag < 0: print("Impossible") break if s[i] in op: stack.append(s[i]) elif s[i] == ">" and stack.pop() != "<": ans += 1 elif s[i] == "}" and stack.pop() != "{": ans += 1 elif s[i] == "]" and stack.pop() != "[": ans += 1 elif s[i] == ")" and stack.pop() != "(": ans += 1 else: if flag != 0: print("Impossible") else: print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() open = {"(", "{", "[", "<"} res = 0 our = [] for elem in s: if elem in open: our.append(elem) elif our: res += abs(ord(elem) - ord(our[-1])) > 2 our.pop() else: print("Impossible") exit() if our: print("Impossible") else: print(res)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() left, ans = list(), 0 def type(ch): if ch in "<>": return 0 if ch in "{}": return 1 if ch in "[]": return 2 if ch in "()": return 3 for ch in s: if ch in "<{[(": left.append(ch) else: if len(left) == 0: print("Impossible") exit(0) ch_left = left.pop() if type(ch) != type(ch_left): ans += 1 print(ans if len(left) == 0 else "Impossible")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() open = "<", "(", "[", "{" close = ">", ")", "]", "}" resp = {close[i]: open[i] for i in (0, 1, 2, 3)} cnt = 0 st = [] flag = True for c in s: if c in open: st.append(c) elif st and st[-1] == resp[c]: st.pop() elif st: st.pop() cnt += 1 else: flag = False break if st: flag = False print(cnt if flag else "Impossible")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING ASSIGN VAR STRING STRING STRING STRING ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() arr = [] l = len(s) flag = False cnt = 0 open = ["{", "[", "<", "("] close = ["}", "]", ">", ")"] dic = {"{": "}", "[": "]", "<": ">", "(": ")"} for i in range(l): if s[i] in open: arr.append(s[i]) else: if arr == []: flag = True break if dic[arr[-1]] != s[i]: cnt += 1 del arr[-1] if flag or arr != []: print("Impossible") else: print(cnt)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR LIST ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
stack = [] def main(): s = str(input()) open = list("<{[(") close = list(">}])") answer = 0 for e in s: if len(stack) == 0: if e in open: stack.append(e) else: print("Impossible") return else: top = len(stack) - 1 if e in open: if stack[top] in close: if open.index(e) != close.index(stack[top]): answer += 1 stack.pop(top) else: stack.append(e) elif stack[top] in close: stack.append(e) else: if close.index(e) != open.index(stack[top]): answer += 1 stack.pop(top) if len(stack) != 0: print("Impossible") else: print(answer) def __starting_point(): main() __starting_point()
ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = list(input()) x = list("<>{}[]()") l = [0] * 4 d = {} k = 0 o, c = 0, 0 f = 1 ans = 0 for t in s: y = x.index(t) yy = y // 2 if y % 2 == 0: if f: k += 1 o += 1 d[k] = yy f = 1 else: if not f: k -= 1 if k <= 0: print("Impossible") exit() c += 1 f = 0 if not d[k] == yy: ans += 1 print(ans if o == c else "Impossible")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
from sys import argv, exit def rstr(): return input() def rint(): return int(input()) def rints(): return [int(i) for i in input().split(" ")] def prnt(*args): if "-v" in argv: print(*args) op = ["[", "{", "(", "<"] cl = ["]", "}", ")", ">"] oppo = {"]": "[", "}": "{", ">": "<", ")": "("} oppo.update(zip(op, cl)) s = rstr() opd = {c: (0) for c in op} shtack = [] opi = 0 count = 0 for c in s: if c in cl: prnt(shtack) if not shtack: print("Impossible") exit(0) elif shtack[-1] == oppo[c]: shtack.pop() else: count += 1 prnt(c, count) shtack.pop() else: shtack.append(c) if shtack: print("Impossible") exit(0) print(count)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF IF STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
d = {"(": ")", "[": "]", "{": "}", "<": ">"} st, k = [], 0 for i in input(): if i in d: st.append(i) elif st: if d[st[-1]] != i: k += 1 st.pop() else: print("Impossible") exit() if st: print("Impossible") exit() print(k)
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
SYMBOLS = {"}": "{", "]": "[", ")": "(", ">": "<"} SYMBOLS_L, SYMBOLS_R = SYMBOLS.values(), SYMBOLS.keys() arr = [] num = 0 left = 0 right = 0 flag = True s = input() for c in s: if c in SYMBOLS_L: arr.append(c) left += 1 elif c in SYMBOLS_R: right += 1 if len(arr) == 0: flag = False break elif arr[-1] == SYMBOLS[c]: arr.pop() else: num += 1 arr.pop() if right != left: flag = False if flag: print(num) else: print("Impossible")
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
def main(): s = input() open_paren = "<([{" close = {"]": "[", ">": "<", ")": "(", "}": "{"} stack = [] ans = 0 imp = False for c in s: if c in open_paren: stack.append(c) else: if not stack: imp = True break if close[c] != stack[-1]: ans += 1 stack.pop() imp = imp or len(stack) != 0 print("Impossible" if imp else ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
R = lambda: list(map(int, input().split())) s = input() ok = True ans = 0 st = [] opn = set("<[({") inv = {"}": "{", ")": "(", ">": "<", "]": "["} for i in s: if i in opn: st.append(i) else: if not st: ok = False break if inv[i] != st[-1]: ans += 1 st.pop() if not ok or st: print("Impossible") else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
import sys s = input() q = [] c = 0 for x in s: if x == "[" or x == "{" or x == "(" or x == "<": q.append(x) elif len(q) > 0 and (ord(x) == ord(q[-1]) + 2 or x == ")" and q[-1] == "("): q.pop(-1) elif len(q) > 0: q.pop(-1) c += 1 else: print("Impossible") sys.exit(0) print(c if len(q) == 0 else "Impossible")
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR STRING
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input() brackets = {"<": ">", "{": "}", "[": "]", "(": ")"} OPENING = "<{[(" CLOSING = ">}])" def main(s): stack = [] res = 0 for c in s: if c in OPENING: stack.append(c) else: if stack: top = stack.pop() if top in OPENING: if brackets[top] != c: res += 1 continue return "Impossible" return res if not stack else "Impossible" print(main(s))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER RETURN STRING RETURN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >. The following definition of a regular bracket sequence is well-known, so you can be familiar with it. Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS. For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not. Determine the least number of replaces to make the string s RBS. -----Input----- The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6. -----Output----- If it's impossible to get RBS from s print Impossible. Otherwise print the least number of replaces needed to get RBS from s. -----Examples----- Input [<}){} Output 2 Input {()}[] Output 0 Input ]] Output Impossible
s = input().strip() braces = { "<": ">", ">": "<", "{": "}", "}": "{", "[": "]", "]": "[", "(": ")", ")": "(", } stack = [] answer = 0 for char in s: if char in "(<{[": stack.append(char) elif char in ")>}]": if not stack: print("Impossible") exit() lastBrace = stack.pop() if char != braces[lastBrace]: answer += 1 if not stack: print(answer) else: print("Impossible")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING