-
LCS 최장 공통 부분 순서 구하기 (Python 코드)
Longest Common Subsequence 최장 공통 부분 순서 ex1) bcdb는 abcbdab의 부분 순서이다. - 연속되지 않아도 됨 ex2) bca는 abcbdab와 bdcaba 두 문자열에 공통적으로 나타나는 부분 순서이다. bcba는 두 문자열에 존재하는 최장 공통 부분 순서이다. LCS의 길이를 구하는 알고리즘 1. 재귀 int LCS(char *X, char *Y, m, n) { if (m = 0 or n = 0) then return 0; else if (X[m - 1] = y[n - 1])then return LCS(X, Y, m - 1, n - 1) + 1; else return max(LCS(X, Y, m-1, n), LCS(X, Y, m, n-1) } 재귀로 LCS를 구하면..
🖥
2022. 5. 17.
-
[Python] Slice로 부분 리스트, 부분 문자열 구하기
슬라이스를 이용하면 된다 Slice: [begin:end:step] range 함수와 같은 구조이다! 부분 리스트 구하기 a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(a) #전체 리스트 출력 print(a[1:]) #1번부터 끝까지 print(a[1:3]) #1번부터 3번 전까지 print(a[:4]) #처음부터 4번 전까지 print("-----------------------------") print(a[::2]) #처음부터 끝까지, 2씩 건너뛰고 print(a[1::2]) #1번부터 끝까지, 2씩 건너뜀 print(a[::-1]) #역순 부분 문자열 구하기 같은 방법으로 슬라이스를 이용하면 된다. s = "pythonstringslice" print(s) print(s[..
🖥
2022. 5. 9.
-
순열, 조합 (Python3)
파이썬으로 순열, 조합, 중복순열, 중복조합 경우의 수 구하기! 순열 n개 중 서로 다른 r개를 뽑아서 줄 세우는 경우의 수 nPr = n! / (n-r)! 조합 n개중 서로 다른 r개를 뽑기만 함 nCr = n! / (n-r)!*r! 순열, 조합을 구하는 방법 #from itertools import permutations, combinations, product, combinations_with_replacement import itertools data = ['A', 'B', 'C'] #순열 print(list(itertools.permutations(data,3))) #조합 print(list(itertools.combinations(data,2))) #중복순열 print(list(itertool..
🖥
2022. 3. 22.