分类 algorithm 中的文章

Union find

并查集 框架如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class UF: def __init__(self, N): self.N = N self._id = [i for i in range(N)] def find(self, p): while p != self._id[p]: p = self._id[p] return p def union(self, p, q): p = self.find(p) q = self.find(q) if p==q: return self._id[p] = q……

阅读全文

MD5 algorithm

MD5 Algorithm 一个最小的MD5算法(md5.c) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 #include <stdint.h> void md5_compress(uint32_t state[static 4], const uint8_t block[static 64])……

阅读全文

Dynamic Programming

Dynamic Programming 1. 最长上升子序列 1 2 3 4 5 6 7 8 9 10 11 12 class Solution: def lengthOfLIS(self, nums: List[int]) -> int: if len(nums) == 0: return 0 dp = [1] * len(nums) L = len(nums) for i in range(L): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j]+1) return max(dp) 2. 最长公共子序列 1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: L1, L2 = len(text1), len(text2) dp = [[0] * (L1+1) for _ in range(L2+1)] for i in range(1, L2+1): for j in range(1, L1+1): if text2[i-1] == text1[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[-1][-1] 3. 三角形最小路……

阅读全文