-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm_implementations.cpp
More file actions
130 lines (119 loc) · 3.58 KB
/
algorithm_implementations.cpp
File metadata and controls
130 lines (119 loc) · 3.58 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
////////////////////////////////////////////////////////////////////////////////
// Combinatorics
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// N-Choose-R / Pascals Triangle
// Runtime: O(n^2) initialize, O(1) get solution
// Usage: Call initNCR();
// Access solution with nCr[n][r];
// Notes: Works for n up to 66 on ull
////////////////////////////////////////////////////////////////////
typedef unsigned long long ull;
ull nCr[68][68]; // Answer stored here
void initNCR() {
for (int i=0; i<68; i++) {
nCr[i][i] = 1;
nCr[i][0] = 1;
}
for (int i=2; i<68; i++) {
for (int j=1; j<i; j++) {
nCr[i][j] = nCr[i-1][j-1] + nCr[i-1][j];
}
}
}
////////////////////////////////////////////////////////////////////
// Factorial
// Runtime: O(n) initialize, O(1) get solution
// Usage: Call initFact();
// Access solution with fact[n];
// Notes: Works for n up to 20 on uint64
// Works for n up to 12 on uint32
////////////////////////////////////////////////////////////////////
typedef unsigned long long ull;
ull fact[21]; // Answer stored here
void initFact() {
fact[0] = 1;
for (int i=1; i<21; i++)
fact[i] = fact[i-1] * i;
}
////////////////////////////////////////////////////////////////////////////////
// Graphs
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Floyd Warshall - All pairs shortest paths
// Runtime: O(n^3) to get all pairs shortest path, O(1) to access
// Usage: Set MAX_N to the maximum number of nodes
// Set N to number of nodes in graph
// Fill in adjacency matrix
// Call floydWarshall();
// Access with W[src][dst];
// Notes: Diagonals should be 0, unconnected should be INF
// We had to run this twice once?
////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <climits>
using namespace std;
#define INF INT_MAX
#define MAX_N // Fill in
int N;
int W[MAX_N][MAX_N];
void reset() { // Optional helper
for (int i=0; i<MAX_N; i++) {
fill(W[i], W[i]+MAX_N, INT_MAX);
W[i][i] = 0;
}
}
void floydWarshall() {
for (int k=0; k<N; k++) {
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (W[i][k]==INF || W[k][j]==INF) continue;
W[i][j] = min(W[i][j], W[i][k]+W[k][j]);
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Strings
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Knuth Morris Pratt - Searches for string w in string s (of length k)
// Runtime: O(k)
// Usage: int ans = KMP(s, w)
// If w in s, then returns index in s that starts w, else s.length()
////////////////////////////////////////////////////////////////////
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
void initKMP(string& w, vi& t)
{
t = vi(w.length());
int i = 2, j = 0;
t[0] = -1; t[1] = 0;
while(i < w.length()) {
if(w[i-1] == w[j]) {
t[i] = j+1; i++; j++;
} else if(j > 0) {
j = t[j];
} else {
t[i] = 0; i++;
}
}
}
int KMP(string& s, string& w)
{
int m = 0, i = 0;
vi t;
initKMP(w, t);
while(m+i < s.length()) {
if(w[i] == s[m+i]) {
i++;
if(i == w.length()) return m;
} else {
m += i-t[i];
if(i > 0) i = t[i];
}
}
return s.length();
}