complete 2098.py 5052.py 14425*.py 14725.py & add 13505.py
This commit is contained in:
8
zeta_python/completed/14425.py
Normal file
8
zeta_python/completed/14425.py
Normal file
@@ -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))
|
||||
44
zeta_python/completed/14425_trie.py
Normal file
44
zeta_python/completed/14425_trie.py
Normal file
@@ -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)))
|
||||
35
zeta_python/completed/14725.py
Normal file
35
zeta_python/completed/14725.py
Normal file
@@ -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)
|
||||
63
zeta_python/completed/2098.py
Normal file
63
zeta_python/completed/2098.py
Normal file
@@ -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))
|
||||
32
zeta_python/completed/5052.py
Normal file
32
zeta_python/completed/5052.py
Normal file
@@ -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")
|
||||
Reference in New Issue
Block a user