-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Problem: Subordinates | ||
# Solution by Kenny Jesús Flores Huamán | ||
# url: https://cses.fi/problemset/task/1674b | ||
|
||
|
||
import sys | ||
|
||
sys.setrecursionlimit(200006) # Aumenta el límite de recursión | ||
|
||
|
||
def dfs(node, tree, subordinates): | ||
count = 0 | ||
for child in tree[node]: | ||
count += dfs(child, tree, subordinates) + 1 | ||
subordinates[node] = count | ||
return count | ||
|
||
|
||
if __name__ == "__main__": | ||
n = int(input()) | ||
ls = list(map(int, input().split())) | ||
|
||
tree = [[] for _ in range(n + 1)] | ||
subordinates = [0] * (n + 1) | ||
|
||
for idx, boss in enumerate(ls, start=2): | ||
tree[boss].append(idx) | ||
|
||
print(tree) | ||
dfs(1, tree, subordinates) | ||
|
||
output = subordinates[1:] | ||
|
||
print(*output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Problem: Two Screens | ||
# Solution by Kenny Jesús Flores Huamán | ||
# URL: https://codeforces.com/problemset/problem/2025A | ||
|
||
def condicion(s: str, t: str, mid: int) -> bool: | ||
for k in range(len(s) + 1): # Probar cada posible longitud de prefijo de s | ||
# Encontrar el mayor prefijo común entre s[:k] y t | ||
m = 0 | ||
while m < len(t) and m < k and s[m] == t[m]: | ||
m += 1 | ||
|
||
# Calcular el tiempo total: | ||
# 1. Escribir los primeros k caracteres de s | ||
# 2. Copiar esos k caracteres a la otra pantalla | ||
# 3. Escribir el resto de s y t | ||
total = k + 1 + (len(s) - k) + (len(t) - m) | ||
|
||
if total <= mid: | ||
return True | ||
return False | ||
|
||
|
||
def solve(s:str,t:str) -> int: | ||
left, right = max(len(s), len(t)), 2*(len(s)+len(t)) | ||
|
||
while left < right: | ||
# Tiempo que tardan las dos secuencias | ||
mid = (left+right) // 2 | ||
|
||
# Si es posible completar las dos secuencias en mid segundos | ||
# Se intenta buscar un menor tiempo | ||
if condicion(s, t, mid): | ||
right = mid | ||
else: | ||
left = mid + 1 | ||
|
||
# A veces puede ser que escribir cada pantalla uno por uno sea más rápido | ||
tt = len(s) + len(t) | ||
return left if tt > left else tt | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
q = int(input()) | ||
for _ in range(q): | ||
s = input() | ||
t = input() | ||
print(solve(s,t)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Problem: Ada and Indexing | ||
# Solution by Kenny Jesús Flores Huamán | ||
# url: https://www.spoj.com/problems/ADAINDEX/ | ||
|
||
import sys | ||
input = sys.stdin.readline | ||
|
||
class TrieNode: | ||
def __init__(self) -> None: | ||
self.children = {} | ||
self.prefix_count = 0 | ||
|
||
class Trie: | ||
def __init__(self) -> None: | ||
self.root = TrieNode() | ||
|
||
def add(self, s: str) -> None: | ||
node = self.root | ||
for c in s: | ||
if c not in node.children: | ||
node.children[c] = TrieNode() | ||
node = node.children[c] | ||
node.prefix_count += 1 | ||
|
||
def search(self, s: str) -> int: | ||
node = self.root | ||
for c in s: | ||
if c not in node.children: | ||
return 0 | ||
node = node.children[c] | ||
return node.prefix_count | ||
|
||
if __name__ == "__main__": | ||
output = [] | ||
|
||
n, q = map(int, input().split()) | ||
trie = Trie() | ||
|
||
for _ in range(n): | ||
trie.add(input().strip()) | ||
|
||
for _ in range(q): | ||
output.append(str(trie.search(input().strip()))) | ||
|
||
sys.stdout.write("\n".join(output) + "\n") |