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 / 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);
@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 / 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 / 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 / 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 / topsort.cpp
Last active March 6, 2018 05:43
topological sort for directed graph
using std::vector;
using std::queue;
vector<int> topsort(const vector<vector<int> > &g) {
int n = g.size();
vector<int> d(n);
for(int i = 0; i < n; i++)
for(int j = 0; j < g[i].size(); j++)
d[g[i][j]]++;
struct TrieNode {
int minVal;
TrieNode *child[2];
TrieNode() {
minVal = inf;
child[0] = child[1] = NULL;
}
};
struct Trie {
const int maxn = 1e5 + 5;
struct Dsu {
struct Node {
int val, fa;
} t[maxn];
void init(int sz) {
for (int i = 0; i < n; i++)
t[i].val = 1, t[i].fa = i;
@maxkibble
maxkibble / binary_indexed_tree.cpp
Last active September 25, 2018 14:14
c++ implementation for date structure: binary indexed tree
struct BinaryIndexedTree {
int up;
long long tree[maxn];
void init(int u) {
up = u;
memset(tree, 0, sizeof(tree));
}
inline int lowbit(int x) { return (x & (-x)); }
@maxkibble
maxkibble / calculator.py
Created June 26, 2018 05:26
python implementation of calculating an infix expression
from operator import add, sub, mul, truediv
class Calculator(object):
op = {'+': add, '-': sub, '*': mul, '/': truediv}
def to_suffix(self, s):
st = []
ret = ''
tokens = s.split()