Skip to content

Commit

Permalink
Añadir nuevas soluciones
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyfh committed Dec 15, 2024
1 parent 7ac9364 commit 0c32490
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
34 changes: 34 additions & 0 deletions CSES/1674.py
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)
48 changes: 48 additions & 0 deletions Codeforces/2025A.py
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))
45 changes: 45 additions & 0 deletions SPOJ/ADAINDEX.py
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")

0 comments on commit 0c32490

Please sign in to comment.