practivceAlgorithm 570

[알고리즘] 편집거리 알고리즘 : 두 문자열의 유사도를 판단

편집거리 알고리즘 두 문자열의 유사도를 판단하는 알고리즘 유사도란? 어떤 문자열을 삽입, 삭제, 변경을 몇 번 해서 동일하게 바꿀 수 있는지의 최솟값. LCS와 같이 2차원 배열을 통해 문자열을 하나씩 비교. 최초는 공집합과 비교해 문자열 길이만큼 1씩 카운트를 준다(공집합에서 그 문자열이 되려면 열마나 삽입되어야 하는가?) 두 문자를 비교해 추가, 삭제, 변경의 소요마다 1씩 카운트. A[i]==B[j]일 때 편집거리 D(i,j) = D(i-1,j-1) 같은 문자가 나왔을 때는 대각선 왼쪽 위의 값을 가져온다. A[i]!=B[j] 면 D(i,j) = min(D(i-1,j),D(i,j-1),D(i-1,j-1)) + 1 즉 수정, 삽입, 삭제를 한 편집거리중 최소값을 가져온다. def editDistanc..

[백준][Python] 17626 Four Squares

bfs로 나머지값보다 작고 첫번째 가능한 제곱수값과 나머지의 차이보다 큰 제곱수들만 조사하는 방식. 좋은걸 깨달았다. bfs의 종료조건은 무조건 위보다 아래가 좋다는 것을.. 아래에 하고 return해야 한분기 덜 돈다. import sys input = sys.stdin.readline from collections import deque def bfs(n): q = deque() q.append((n,0)) visited[n] = True while q: r, cnt = q.popleft() flag = 1 for square in squares: next_r = r-square if next_r > 0 and not visited[next_r]: if flag: first = next_r flag ..

[백준][Python] 16928 뱀과 사다리게임

portal만들고 중복체크만해줌. import sys input = sys.stdin.readline from collections import deque def bfs(start): q = deque() q.append((start,0)) visited[start] = True while q: cur_node, cnt = q.popleft() for i in range(1,7): next_node = cur_node+i if next_node >= 100: return cnt+1 if visited[next_node]: continue visited[next_node] = True if next_node in port: if not visited[port[next_node]]: visited[port[..