diff --git a/zeta_python/13505.py b/zeta_python/13505.py new file mode 100644 index 0000000..29315d5 --- /dev/null +++ b/zeta_python/13505.py @@ -0,0 +1,50 @@ +import sys + +input = sys.stdin.readline + + +def get_max_xor(N, S): + trie = [[0, 0] for _ in range(150000)] + + cnt = 0 + for s in S: + idx = 0 + for b in s: + b = int(b) + if trie[idx][b]: + idx = trie[idx][b] + else: + trie[idx][b] = cnt + 1 + idx = cnt + 1 + cnt += 1 + n = [] + D = [(0, 0, 0, 0)] + while D: + depth, idx1, idx2, xor = D.pop() + if depth == 30: + n.append(xor) + + flag = False + + if trie[idx1][0] and trie[idx2][1]: + D.append((depth + 1, trie[idx1][0], trie[idx2][1], xor + 1 << (29 - depth))) + flag = True + if trie[idx1][1] and trie[idx2][0]: + D.append((depth + 1, trie[idx1][1], trie[idx2][0], xor + 1 << (29 - depth))) + flag = True + print(D) + if flag: + continue + + if trie[idx1][0] and trie[idx2][0]: + D.append((depth + 1, trie[idx1][0], trie[idx2][0], xor)) + if trie[idx1][1] and trie[idx2][1]: + D.append((depth + 1, trie[idx1][1], trie[idx2][1], xor)) + print(n) + return max(n) + + +if __name__ == "__main__": + N = int(input()) + S = list(map(lambda x: format(int(x), "b").zfill(30), input().split())) + print(get_max_xor(N, S)) diff --git a/zeta_python/completed/14425.py b/zeta_python/completed/14425.py new file mode 100644 index 0000000..247632c --- /dev/null +++ b/zeta_python/completed/14425.py @@ -0,0 +1,8 @@ +import sys + +input = sys.stdin.readline + +if __name__ == "__main__": + N, M = map(int, input().split()) + S = {input().rstrip() for _ in range(N)} + print(sum(1 for _ in range(M) if input().rstrip() in S)) diff --git a/zeta_python/completed/14425_trie.py b/zeta_python/completed/14425_trie.py new file mode 100644 index 0000000..935d25b --- /dev/null +++ b/zeta_python/completed/14425_trie.py @@ -0,0 +1,44 @@ +import sys + +input = sys.stdin.readline + +trie = [[0 for _ in range(26)] for _ in range(5050505)] +fins = [0 for _ in range(5050505)] +cnt = 1 + + +def convert(c: str) -> int: + return ord(c) - ord("a") + + +# trie +def insert(s: str): + global cnt + cur = 0 + for c in s: + nxt = convert(c) + if not trie[cur][nxt]: + trie[cur][nxt] = cnt + cnt += 1 + cur = trie[cur][nxt] + fins[cur] = 1 + + +def query(s): + cur = 0 + for c in s: + nxt = convert(c) + if not trie[cur][nxt]: + if s == "sundaycoding": + print(c) + return 0 + cur = trie[cur][nxt] + return fins[cur] + + +if __name__ == "__main__": + N, M = map(int, input().split()) + for _ in range(N): + insert(input().strip()) + + print(sum(query(input().strip()) for _ in range(M))) diff --git a/zeta_python/completed/14725.py b/zeta_python/completed/14725.py new file mode 100644 index 0000000..5a0d7f6 --- /dev/null +++ b/zeta_python/completed/14725.py @@ -0,0 +1,35 @@ +import sys + +input = sys.stdin.readline + +MAX = 150505 + + +def print_structrue(N, S): + trie = [dict() for _ in range(MAX)] + + cnt = 0 + + for s in S: + idx = 0 + for w in s: + if trie[idx].get(w, 0): + idx = trie[idx][w] + else: + trie[idx][w] = cnt + 1 + idx = cnt + 1 + cnt += 1 + + D = [(0, 0, None)] + while D: + idx, depth, word = D.pop() + if depth: + print("--" * (depth - 1) + word) + for key, value in sorted(trie[idx].items())[::-1]: + D.append((value, depth + 1, key)) + + +if __name__ == "__main__": + N = int(input()) + S = [input().rstrip().split()[1:] for _ in range(N)] + print_structrue(N, S) diff --git a/zeta_python/completed/2098.py b/zeta_python/completed/2098.py new file mode 100644 index 0000000..5b54881 --- /dev/null +++ b/zeta_python/completed/2098.py @@ -0,0 +1,63 @@ +import sys +import heapq as hq + +input = sys.stdin.readline + +INF: int = 1 << 32 +FULL: int + + +def vis_full(vis) -> int: + return vis == FULL + + +def vis_add(vis, i) -> int: + return vis | (1 << i) + + +def vis_is_in(vis, i) -> bool: + return vis & (1 << i) != 0 + + +def get_min_cost(N: int, W: list[list[int]]): + start = 0 + st_vis = 0 + st_vis = vis_add(st_vis, 0) + + M = [[-1 for _ in range(FULL + 1)] for _ in range(N)] + stack = [(0, st_vis)] + + while stack: + now, vis = stack[-1] + if vis_full(vis): + stack.pop() + if W[now][start] != 0: + M[now][vis] = W[now][start] + else: + M[now][vis] = INF + continue + + if M[now][vis] == -1: + M[now][vis] += 1 + for i in range(N): + if (not vis_is_in(vis, i)) and W[now][i] != 0: + if M[i][vis_add(vis, i)] <= 0: + stack.append((i, vis_add(vis, i))) + + elif M[now][vis] == 0: + stack.pop() + s = [] + for i in range(N): + if not vis_is_in(vis, i) and W[now][i] != 0: + s.append(M[i][vis_add(vis, i)] + W[now][i]) + M[now][vis] = min(s) if s else INF + else: + continue + return M[0][st_vis] + + +if __name__ == "__main__": + N = int(input()) + W = [list(map(int, input().split())) for _ in range(N)] + FULL = (1 << N) - 1 + print(get_min_cost(N, W)) diff --git a/zeta_python/completed/5052.py b/zeta_python/completed/5052.py new file mode 100644 index 0000000..6253467 --- /dev/null +++ b/zeta_python/completed/5052.py @@ -0,0 +1,32 @@ +import sys + +input = sys.stdin.readline + +def is_consist(N: int, S: list) -> bool: + S.sort() + trie = [[0] * 10 for _ in range(155050)] + fin = [0] * 155050 + cnt = 0 + for s in S: + idx = 0 + for c in s: + if fin[idx]: + return False + if trie[idx][int(c)]: + idx = trie[idx][int(c)] + else: + trie[idx][int(c)] = cnt + 1 + idx = cnt + 1 + cnt += 1 + fin[idx] = 1 + + return True + + + +if __name__ == '__main__': + T = int(input()) + for _ in range(T): + N = int(input()) + S = [input().rstrip() for _ in range(N)] + print("YES" if is_consist(N, S) else "NO") \ No newline at end of file