Skip to content

Instantly share code, notes, and snippets.

@Gloveman
Gloveman / 9251 LCS.py
Created July 11, 2025 08:39
Longest common substring #python #algorithm
import sys
input = sys.stdin.readline
s1=input().strip()
s2=input().strip()
dp=[[0 for _ in range(len(s2)+1)] for _ in range(len(s1)+1)]
for i in range(1,len(s1)+1):
for j in range(1,len(s2)+1):
if s1[i-1] == s2[j-1]:
dp[i][j]=dp[i-1][j-1]+1
@Gloveman
Gloveman / 11866 Josephus problem 0.py
Created July 11, 2025 08:08
Josephus problem #python #algorithm
import sys
n,K=map(int,sys.stdin.readline().split())
k=0
people=[i+1 for i in range(n)]
print('<',end='')
for _ in range(n-1):
idx=(k+K-1)%len(people)
print(f'{people.pop(idx)}, ',end='')
k=idx%len(people)
print(f'{people.pop()}>')
@Gloveman
Gloveman / 1012 Organic Cabbage.py
Last active July 11, 2025 08:07
N of connected component #python #algorithm
Grid=[[0 for _ in range(n)] for _ in range(m)]
visited=[[False for _ in range(n)] for _ in range(m)]
count=0
for _ in range(k):
x,y=map(int,sys.stdin.readline().split())
Grid[x][y]=1
for i in range(m):
for j in range(n):
if Grid[i][j] and not visited[i][j]:
count+=1
@Gloveman
Gloveman / 2805 cutting trees.py
Last active July 11, 2025 08:07
Basic binary search #python #algorithm
import sys
input=sys.stdin.readline
n,m = map(int,input().split())
trees=list(map(int,input().split()))
start, end = 0, max(trees)
result = 0
while start <= end:
mid = (start + end) // 2
cur_m = sum(x - mid for x in trees if x > mid)
@Gloveman
Gloveman / 13549 hide and seek.py
Last active July 11, 2025 08:07
0-1 BFS #python #algorithm
import sys
from collections import deque
input = sys.stdin.readline
n,k=map(int,input().split())
if n>=k:
print(n-k)
else:
queue=deque()
dist=[100002] * 100001
dist[n]=0