Skip to content

Instantly share code, notes, and snippets.

@ogzd
ogzd / stack_queue.cpp
Created March 12, 2013 18:40
Stack & Queue Implementation
class MyStack
{
private:
int pos;
int a[MAXSZ];
public:
MyStack() : pos(0){}
void push(int val)
@ogzd
ogzd / BIT.cpp
Created March 3, 2013 13:50
Binary Indexed Tree
#define LOGSZ 17
int tree[(1<<LOGSZ)+1];
int N = (1<<LOGSZ);
// add v to value at x
void add(int x, int v) {
while(x <= N) {
tree[x] += v;
x += (x & -x);
@ogzd
ogzd / SCC.cpp
Last active December 14, 2015 07:29
Kosaraju's Algorithm for finding SCCs
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <vector>
@ogzd
ogzd / MST.cpp
Last active December 14, 2015 07:19
Kruskal's Algorithm using union-find
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <vector>
@ogzd
ogzd / KMP.cpp
Last active December 14, 2015 04:29
KMP String matching
// implementation of Knuth-Morris-Pratt string matching algorithm
// complexity O(n)
// t: text, p: pattern
void kmp(char t[], char p[])
{
int n = strlen(t);
int m = strlen(p);
f[0] = 0;
f[1] = 0;
@ogzd
ogzd / RMQ.cpp
Last active December 14, 2015 04:08
Range Minimum Query
int rmq[70]; //keeps indices
int ax[10] = {1,4,5,6,7,2,0,3,9,11}; //keeps values
int bx[10]; //keeps node indices for each value in ax
int n;
// init(1, 0, n-1);
void init(int node, int b, int e) {
if(b == e) {
rmq[node] = b;
bx[b] = node;