Skip to content

Commit b5c39c9

Browse files
author
kaidul-EB
committedDec 29, 2016
Game Theory, Number theory, String algorithms added
1 parent 56f5a8f commit b5c39c9

34 files changed

+2173
-1
lines changed
 

‎.DS_Store

4 KB
Binary file not shown.

‎Josephus_Recurrence.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
General case for any k
3+
f(n, k) = ((f(n - 1, k) + k - 1) mod n) + 1, f(1, k) = 1 (when n = 1...8)
4+
*/
5+
int josephus(int n, int k) {
6+
if(n == 1) return 0;
7+
return (josephus(n - 1, k) + k) % n;
8+
}
9+
10+
/*
11+
When k = 2
12+
Proof: wikipedia
13+
2 * (n - 2^floor(log2n)) + 1
14+
*/
15+
int josephusfor2(int n) {
16+
return 2 * (n - pow(2, floor(log((double) n) / log(2.0)))) + 1;
17+
}
18+
19+
/*
20+
another proof for k = 2
21+
source: wikipedia
22+
n = 1b1b2b3b4...bm (bit representaion), ans = b1b2b3b4...bm1 (shifting leftmost bit into rightmost)
23+
*/
24+
int josephusfor2_bit(int n) {
25+
n <<= 1;
26+
n |= 1;
27+
int bit = floor((double)log(n) / log(2.0));
28+
int var = (1 << bit);
29+
--var;
30+
n &= var;
31+
return n;
32+
}

‎Power.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
int powerUtil(int base, int exp, int mod) {
2+
if(exp == 0) return 1;
3+
int ret = powerUtil(base, exp / 2, mod) % mod;
4+
ret = 1LL * ret * ret % mod;
5+
if(exp & 1) {
6+
ret = 1LL * ret * base % mod;
7+
}
8+
return ret;
9+
}
10+
11+
double power(int base, int exp, int mod = 1000000007) {
12+
if(exp < 0) {
13+
if(base == 0) return DBL_MAX; // undefined
14+
return 1 / (double) powerUtil(base, -exp, mod); // return type will be double/long double then
15+
}
16+
return powerUtil(base, exp, mod);
17+
}
18+
19+
// Modular mutiplicative inverse
20+
int modInv(int base, int mod = 1000000007) {
21+
return powerUtil(base, mod - 2, mod);
22+
}

‎README.md

+70-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,88 @@
1-
## Classical Data Structures and Algorithms
1+
## Data Structures and Algorithms for Online Programming Contest
2+
3+
### Data Structure
4+
+ [Union Find(Disjoint Set)](union_find.cpp)
5+
6+
### Dynamic Programming
27

38
### String Algorithm
9+
+ [Knuth-Morris-Pratt’s Algorithm](knuth.cpp)
10+
+ [Rabin Karp Pattern Searching](rabin_karp.cpp)
11+
+ [Z Algorithm](z.cpp)
412
+ [Finite Automata Pattern Searching](Finite_Automata_Pattern_Searching.cpp)
13+
+ [Trie (Prefix/Radix Tree)](Trie.cpp)
14+
+ [Longest Common Subsequence](lcs.cpp)
15+
+ [Edit Distance](edit_distance.cpp)
16+
+ [Longest Palindromic Subsequence](lps.cpp)
17+
+ [Suffix Array](suffix_lcp.cpp)
18+
+ [Longest Common Prefix](suffix_lcp.cpp)
519
+ [Minimum Expression](Minimum_expression.cpp)
20+
+ [Suffix Automata](suffix_automata.cpp)
21+
22+
### Graph Theory
23+
+ [Lowest Common Ancestor(sparse table)](lca.cpp)
24+
625

726
### Dynamic Programming
827
+ [Digit Dp I](Digit_DP_I.cpp)
928

29+
### Mathematics
30+
+ [Power Function(Big mod)](Power.cpp)
31+
+ [Modular Mutiplicative Inverse(using Big mod)](Power.cpp)
32+
+ [Prime(Sieve of Erathonesis)](prime.cpp)
33+
+ [Segmented Sieve of Erathonesis](prime.cpp)
34+
+ [Prime factorization(using Sieve)](prime.cpp)
35+
+ [Prime factorization](prime.cpp)
36+
+ [Primality Test(School method)](primality_test.cpp)
37+
+ [Miller–Rabin Primality Test](primality_test.cpp)
38+
+ [Extended Euclid](extended_euclid.cpp)
39+
+ [Linear Diophatine Equation](extended_euclid.cpp)
40+
+ [Modular Mutiplicative Inverse(using Extended Euclid)](extended_euclid.cpp)
41+
+ [Matrix Exponenciation](matrix_exp.xpp)
42+
+ [Floyd Cycle Finding Algorithm](floyd_cycle_finding.cpp)
43+
+ [Big Integer](big_integer.cpp)
44+
+ [Josephus Recurrence](Josephus_Recurrence.cpp)
45+
46+
### Combinotorics
47+
+ [Factorial](factorial.cpp)
48+
+ [nCr](ncr.cpp)
49+
+ [De-arrangement](dearrangement.cpp)
50+
51+
### Game Theory
52+
+ [Game Tree(Memorization)](game_tree.cpp)
53+
+ [Nim](nim.cpp)
54+
+ [Misère Nim](nim.cpp)
55+
+ [Nimble Nim](nim.cpp)
56+
+ [Poker Nim](nim.cpp)
57+
+ [Prime Power Nim](nim.cpp)
58+
+ [Spagrue Grundy Problem](grundy.cpp)
59+
+ [Grundy Variant: Zero Nim Game](grundy.cpp)
60+
+ [Grundy Variant: Coins on Chessboard](grundy.cpp)
61+
+ [Green HackenBush(Colon Principle)](hackenbush.cpp)
62+
63+
### Binary Search
64+
65+
+ [Binary Search](binary_search.cpp)
66+
+ [Lower Bound](binary_search.cpp)
67+
+ [Upper Bound](binary_search.cpp)
68+
+ [Equal Range](binary_search.cpp)
69+
70+
### Hashing
71+
+ [Double Hashing](double_hashing.cpp)
72+
+ [String Hashing by Map](map_hashing.cpp)
73+
+ [Berenstain String Hashing](berenstain_hashing.cpp)
74+
+ [Rolling Hash](rabin_karp.cpp)
75+
1076
### Sorting
1177
+ [Merge Sort](Merge_Sort.cpp)
1278
+ [Quick Sort](Quick_Sort.cpp)
79+
+ [Heap Sort](heap_sort.cpp)
1380
+ [Bubble Sort](Bubble_Sort.cpp)
1481
+ [Insertion Sort](Insertion_Sort.cpp)
1582
+ [Selection Sort](Selection_Sort.cpp)
1683
+ [Bucket Sort](Bucket_Sort.cpp)
1784
+ [Count Sort](Count_Sort.cpp)
1885
+ [Radix Sort](Radix_Sort.cpp)
1986
+ [Pancake Sort](Pancake_Sort.cpp)
87+
88+
+ [Template](template.cpp)

‎Trie.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const static int SIZE = 30;
2+
const static int NOT_FOUND = -1;
3+
class trie {
4+
private:
5+
struct trieNode {
6+
int mark;
7+
trieNode* children[SIZE];
8+
trieNode(): mark(NOT_FOUND) {
9+
for(int i = 0; i < SIZE; ++i) {
10+
children[i] = nullptr;
11+
}
12+
}
13+
~trieNode() {
14+
for(int i = 0; i < MAX; ++i) {
15+
delete children[i];
16+
children[i] = nullptr;
17+
}
18+
}
19+
};
20+
trieNode* root;
21+
public:
22+
trie(): root(new trieNode()) {
23+
}
24+
~trie() {
25+
delete root;
26+
root = nullptr;
27+
}
28+
29+
void insert(string const& key, int id) {
30+
trieNode* pCrawl = root;
31+
for(int i = 0; i < key.length(); ++i) {
32+
int indx = key[i] - 'a';
33+
if(!pCrawl->children[indx]) {
34+
pCrawl->children[indx] = new trieNode();
35+
}
36+
pCrawl = pCrawl->children[indx];
37+
}
38+
pCrawl->mark = id;
39+
}
40+
41+
int search(string const& key) {
42+
trieNode *pCrawl = root;
43+
for(int i = 0; i < key.length(); ++i) {
44+
int indx = key[i] - 'a';
45+
if(!pCrawl->children[indx]) {
46+
return NOT_FOUND;
47+
}
48+
pCrawl = pCrawl->children[indx];
49+
}
50+
return pCrawl->mark;
51+
}
52+
};

‎berenstain_hashing.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Source: Zobayer Blog
3+
Must use unsigned type.
4+
hash = hash * 33 ^ c, reliable hash
5+
*/
6+
7+
#define u64 unsigned long long
8+
9+
u64 hash(string const& s) {
10+
u64 hash = 0;
11+
for(int i = 0; i < (int)s.length(); i++) {
12+
int c = s[i];
13+
hash = ((hash << 5) + hash) ^ c;
14+
}
15+
return hash;
16+
}

‎big_integer.cpp

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
struct BigInt {
2+
// representations and structures
3+
string a; // to store the digits
4+
int sign; // sign = -1 for negative numbers, sign = 1 otherwise
5+
6+
// constructors
7+
BigInt() {} // default constructor
8+
BigInt( string b ) {
9+
(*this) = b; // constructor for string
10+
}
11+
12+
// some helpful methods
13+
int size() { // returns number of digits
14+
return a.size();
15+
}
16+
BigInt inverseSign() { // changes the sign
17+
sign *= -1;
18+
return (*this);
19+
}
20+
BigInt normalize( int newSign ) { // removes leading 0, fixes sign
21+
for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- )
22+
a.erase(a.begin() + i);
23+
sign = ( a.size() == 1 && a[0] == '0' ) ? 1 : newSign;
24+
return (*this);
25+
}
26+
27+
// assignment operator
28+
void operator = ( string b ) { // assigns a string to BigInt
29+
a = b[0] == '-' ? b.substr(1) : b;
30+
reverse( a.begin(), a.end() );
31+
this->normalize( b[0] == '-' ? -1 : 1 );
32+
}
33+
34+
// conditional operators
35+
bool operator < ( const BigInt &b ) const { // less than operator
36+
if( sign != b.sign ) return sign < b.sign;
37+
if( a.size() != b.a.size() )
38+
return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size();
39+
for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] )
40+
return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i];
41+
return false;
42+
}
43+
bool operator == ( const BigInt &b ) const { // operator for equality
44+
return a == b.a && sign == b.sign;
45+
}
46+
47+
48+
49+
// mathematical operators
50+
BigInt operator + ( BigInt b ) { // addition operator overloading
51+
if( sign != b.sign ) return (*this) - b.inverseSign();
52+
BigInt c;
53+
for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ) {
54+
carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0);
55+
c.a += (carry % 10 + 48);
56+
carry /= 10;
57+
}
58+
return c.normalize(sign);
59+
}
60+
BigInt operator - ( BigInt b ) { // subtraction operator overloading
61+
if( sign != b.sign ) return (*this) + b.inverseSign();
62+
int s = sign;
63+
sign = b.sign = 1;
64+
if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s);
65+
BigInt c;
66+
for( int i = 0, borrow = 0; i < a.size(); i++ ) {
67+
borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
68+
c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
69+
borrow = borrow >= 0 ? 0 : 1;
70+
}
71+
return c.normalize(s);
72+
}
73+
BigInt operator * ( BigInt b ) { // multiplication operator overloading
74+
BigInt c("0");
75+
for( int i = 0, k = a[i] - 48; i < a.size(); i++, k = a[i] - 48 ) {
76+
while(k--) c = c + b; // ith digit is k, so, we add k times
77+
b.a.insert(b.a.begin(), '0'); // multiplied by 10
78+
}
79+
return c.normalize(sign * b.sign);
80+
}
81+
BigInt operator / ( BigInt b ) { // division operator overloading
82+
if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
83+
BigInt c("0"), d;
84+
for( int j = 0; j < a.size(); j++ ) d.a += "0";
85+
int dSign = sign * b.sign;
86+
b.sign = 1;
87+
for( int i = a.size() - 1; i >= 0; i-- ) {
88+
c.a.insert( c.a.begin(), '0');
89+
c = c + a.substr( i, 1 );
90+
while( !( c < b ) ) c = c - b, d.a[i]++;
91+
}
92+
return d.normalize(dSign);
93+
}
94+
BigInt operator % ( BigInt b ) { // modulo operator overloading
95+
if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
96+
BigInt c("0");
97+
b.sign = 1;
98+
for( int i = a.size() - 1; i >= 0; i-- ) {
99+
c.a.insert( c.a.begin(), '0');
100+
c = c + a.substr( i, 1 );
101+
while( !( c < b ) ) c = c - b;
102+
}
103+
return c.normalize(sign);
104+
}
105+
106+
// faster output method
107+
void _print() {
108+
if( sign == -1 ) putchar('-');
109+
for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
110+
}
111+
};
112+
113+
ostream& operator << (ostream& os, const BigInt& obj) {
114+
if( obj.sign == -1 ) os << "-";
115+
for( int i = obj.a.size() - 1; i >= 0; i--) os << obj.a[i];
116+
return os;
117+
}

‎binary_search.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
int lowerBound(vector<int>& arr, int target) {
2+
int n = (int)arr.size();
3+
int left = 0, right = n - 1;
4+
int indx = -1;
5+
while(left <= right) {
6+
int mid = left + (right - left) / 2;
7+
if(arr[mid] == target) {
8+
indx = mid;
9+
}
10+
if(arr[mid] < target) {
11+
left = mid + 1;
12+
} else { // arr[mid] >= target
13+
right = mid - 1;
14+
}
15+
}
16+
return indx;
17+
}
18+
19+
// different from C++ upper_bound. return target's index inclusive
20+
int upperBound(vector<int>& arr, int target) {
21+
int n = (int)arr.size();
22+
int left = 0, right = n - 1;
23+
int indx = -1;
24+
while(left <= right) {
25+
int mid = left + (right - left) / 2;
26+
if(arr[mid] == target) {
27+
indx = mid;
28+
}
29+
if(arr[mid] <= target) {
30+
left = mid + 1;
31+
} else { // arr[mid] > target
32+
right = mid - 1;
33+
}
34+
}
35+
return indx;
36+
}
37+
38+
pair<int, int> equalRange(vector<int>& arr, int target) {
39+
int lbound = lowerBound(arr, target);
40+
int ubound = upperBound(arr, target);
41+
return pair<int, int>{lbound, ubound};
42+
}
43+
44+
int binarySearch(vector<int>& arr, int target) {
45+
int lbound = lowerBound(arr, target);
46+
return lbound;
47+
}

‎dearrangement.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int d(int n) {
2+
if(n == 2) return 1;
3+
if(n == 1) return 0;
4+
if(dp[n] != -1) return dp[n];
5+
return dp[n] = (n - 1) * (d(n - 1) + d(n - 2));
6+
}

0 commit comments

Comments
 (0)
Please sign in to comment.