Skip to content

Instantly share code, notes, and snippets.

View maxkibble's full-sized avatar

Qi Shen maxkibble

  • Peking University,Beijing
View GitHub Profile
@maxkibble
maxkibble / traverse.py
Created February 21, 2018 02:51
traverse all files in a root directory
import os
def dfs(root_path):
for ele in os.listdir(root_path):
path = os.path.join(root_path, ele)
if os.path.isfile(path):
# do something...
else:
dfs(path)
@maxkibble
maxkibble / dijkstra.cpp
Last active October 18, 2017 12:48
heap-optimized Dijkstra algorithm
/*
* INPUT: graph G(V, E) with n vertix and m edges, starting vertex s
* OUTPUT: shortest distance from s to each node, stored in array d[MAX]
* TIME COMPLEXITY: O((m + n) * lg(n))
* SPACE COMPLEXITY: O(n^2)
*/
const int MAX = 205;
const int INF = 0x7fffffff;
@maxkibble
maxkibble / suffix_array.cpp
Last active June 10, 2018 07:00
suffix array
/*
* TIME COMPLEXITY: O(n * lgn)
* SPACE COMPLEXITY: O(n)
*/
const int maxn = 2e4 + 10;
// all indexes start from 0
struct SuffixArray {
int len, str[maxn], sa[maxn], rank[maxn], height[maxn], bucket[maxn];
@maxkibble
maxkibble / kmp.cpp
Last active June 10, 2018 06:57
KMP algorithm in string manipulation
/*
* INPUT: two strings: W and T
* OUTPUT: a integer vector, containing subscripts of T that W appears as sub-string
* TIME COMPLEXITY: O(|W| + |T|)
* SPACE COMPLEXITY: O(|W| + |T|)
*/
class Kmp {
std::string word, text;
int w_len, t_len;
@maxkibble
maxkibble / min_representation.cpp
Last active March 6, 2018 05:50
minimum representation algorithm in string manipulation
/*
* INPUT: a string and its length;
* OUTPUT: an integer n, after n-bit cyclic shift left, the new string is minimum in dictionary order
* TIME COMPLEXITY: O(n)
* SPACE COMPLEXITY: O(n)
*/
int MinRepresentation(char *s) {
int len = strlen(s);
memcpy(s + len, s, len);