├── Data Structures ├── Binary Indexed Tree.cpp ├── Fraction Class.cpp ├── Minimum Subsequent Value.cpp ├── Minimum Subsequent Value.cpp~ ├── Range Minimum Query ( Frenwick Tree ).cpp └── Splay Tree.cpp ├── Geometry ├── Intersections │ └── intersections.cpp ├── Others │ └── atan2 Adapter.cpp ├── Polygon related codes │ ├── area of a 2D polygon.cpp │ ├── convex hull ( 2D ).cpp │ └── convex hull (3D).cpp ├── Structures & classes used in my code │ ├── line.cpp │ ├── point .cpp │ ├── point(3D).cpp │ └── segment.cpp └── geometry template extension.cpp ├── Graph Algorithms & Data Structures ├── Bipartite Matching ( Hopcroft Karp ).cpp ├── Maximum Flow ( Dinic's Algorithm ).cpp ├── Minimum Cost Maximum Flow.cpp └── Strongly Connected Component ( Tarjan ) .cpp ├── Mathematics ├── Big Modular Exponentiation.cpp ├── Chinese Remainder Theorem.cpp └── Matrix Exponentiation.cpp ├── Miscellaneous Codes ├── Fast Input.cpp └── Roman - Decimal Converter ( Both way ).cpp ├── README.md ├── Sample Codes └── Knuth Optimization ( Uva 10304 ).cpp ├── String Algorithms & Data Structures ├── KMP.cpp ├── Manacher.cpp ├── Minimum Lexicographical Rotation O ( N ) .cpp ├── String Class Tokenization.cpp ├── Suffix Array.cpp ├── Suffix Automata.cpp └── Trie.cpp └── Templates ├── Debug Extensions.cpp ├── google codejam template.cpp ├── online contest template (C++11).cpp └── online contest template.cpp /Data Structures/Binary Indexed Tree.cpp: -------------------------------------------------------------------------------- 1 | template class binaryIndexedTree{ 2 | vector tree; 3 | int mV; 4 | public: 5 | binaryIndexedTree(){mV=0;} 6 | binaryIndexedTree(int size){ 7 | tree=vector(size+1); 8 | mV=size; 9 | } 10 | inline void resize(int size){ 11 | tree.clr; 12 | tree=vector(size+1); 13 | mV=size; 14 | } 15 | inline void upd(int idx,T v=1){ 16 | while(idx<=mV){ 17 | tree[idx]+=v; 18 | idx+=idx&-idx; 19 | } 20 | } 21 | inline T get(int idx){ 22 | T ret=0; 23 | while(idx){ 24 | ret+=tree[idx]; 25 | idx-=idx&-idx; 26 | } 27 | return ret; 28 | } 29 | inline T getS(int idx){ 30 | T ret=tree[idx]; 31 | if(idx){ 32 | int z=idx-(idx&-idx); 33 | idx--; 34 | while(idx!=z){ 35 | ret-=tree[idx]; 36 | idx-=idx&-idx; 37 | } 38 | } 39 | return ret; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Data Structures/Fraction Class.cpp: -------------------------------------------------------------------------------- 1 | class Fraction { 2 | public: 3 | long long N,D; 4 | Fraction(long long num=0, long long den=1) : N(num), D(den) { 5 | long long tmp = __gcd(N,D); 6 | if ((tmp < 0) != (den < 0)) 7 | tmp = -tmp; 8 | N /= tmp; 9 | D /= tmp; 10 | } 11 | bool operator<(const Fraction& b) const { 12 | return (N*b.D < b.N*D); 13 | } 14 | bool operator==(const Fraction& b) const { 15 | return (N == b.N && D == b.D); 16 | } 17 | bool operator!=(const Fraction& b) const { 18 | return (N != b.N || D != b.D); 19 | } 20 | Fraction operator+(const Fraction& b) const { 21 | return Fraction(N*b.D + D*b.N, D*b.D); 22 | } 23 | Fraction operator-(const Fraction& b) const { 24 | return Fraction(N*b.D - D*b.N, D*b.D); 25 | } 26 | Fraction operator*(const Fraction& b) const { 27 | return Fraction(N*b.N, D*b.D); 28 | } 29 | Fraction operator/(const Fraction& b) const { 30 | return Fraction(N*b.D, D*b.N); 31 | } 32 | }; 33 | ostream& operator<<(ostream& out, Fraction& f) { 34 | out << f.N; 35 | if (f.D > 1) 36 | out << "/" << f.D; 37 | return out; 38 | } 39 | -------------------------------------------------------------------------------- /Data Structures/Minimum Subsequent Value.cpp: -------------------------------------------------------------------------------- 1 | struct MyQ{ 2 | deque D, Min; 3 | 4 | void push(int val) { 5 | //Pushes in element at the back of Queue 6 | //Complexity - O(1), amortized 7 | 8 | D.push_back(val); 9 | 10 | while(!Min.empty() && Min.back() > val) Min.pop_back(); 11 | 12 | Min.push_back(val); 13 | } 14 | 15 | void pop() { 16 | //Pops an element from the front of Queue 17 | //Complexity - O(1), amortized 18 | 19 | if(Min.front() == D.front()) 20 | Min.pop_front(); 21 | 22 | D.pop_front(); 23 | } 24 | 25 | int getMin() { 26 | //Returns minimum of current existing elements of queue 27 | //Complexity - O(1) 28 | 29 | return Min.front(); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Data Structures/Minimum Subsequent Value.cpp~: -------------------------------------------------------------------------------- 1 | struct MyQ{ 2 | deque D, Min; 3 | 4 | void push(int val) { 5 | //Pushes in element at the back of Queue 6 | //Complexity - O(1), amortized 7 | 8 | D.push_back(val); 9 | 10 | while(!Min.empty() && Min.back() > val) Min.pop_back(); 11 | 12 | Min.push_back(val); 13 | } 14 | 15 | void pop() { 16 | //Pops an element from the front of Queue 17 | //Complexity - O(1), amortized 18 | 19 | if(Min.front() == D.front()) 20 | Min.pop_front(); 21 | 22 | D.pop_front(); 23 | } 24 | 25 | int getMin() { 26 | //Returns minimum of current existing elements of queue 27 | //Complexity - O(1) 28 | 29 | return Min.front(); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Data Structures/Range Minimum Query ( Frenwick Tree ).cpp: -------------------------------------------------------------------------------- 1 | /// Author : Bidhan Roy 2 | /// Range Minimum Query using Frenweek Tree ( Binary indexed tree ) 3 | /// Source : https://github.com/BidhanRoy/Algorithm-Code-Library 4 | /// 1 based 5 | 6 | template < typename Int > 7 | class rangeMinimumQuery { 8 | 9 | Int Inf; /// infinite value 10 | Int *Tree; /// vector containing the actual tree 11 | unsigned size; /// size of the tree 12 | 13 | public : 14 | 15 | rangeMinimumQuery ( unsigned _size ) : Inf ( numeric_limits< Int >::max() ) , size( _size ) { 16 | /// size of tree should be double of original array 17 | Tree = new Int [ ( size << 1 ) + 1 ]; 18 | fill( Tree , Tree + (size<<1) + 1 , Inf ); 19 | } 20 | 21 | ~rangeMinimumQuery () { 22 | delete( Tree ); 23 | size = Inf = 0; 24 | } 25 | 26 | void set( unsigned idx, Int v ) { 27 | 28 | /// Sets value of index 'idx' to v, 29 | /// if the existing value is already greater. 30 | /// 1 Based 31 | 32 | if( idx == 0 ) { 33 | cerr << "Indexes should be 1 based." << endl; 34 | throw ; 35 | } 36 | 37 | idx += size; 38 | 39 | while( idx ) { 40 | Tree[ idx ] = min( Tree[ idx ] , v ); 41 | idx >>= 1; 42 | } 43 | } 44 | 45 | Int get( unsigned L, unsigned R ) { 46 | 47 | /// L, R are 1 based 48 | 49 | Int ret = Inf; 50 | L += size; R += size + 1; /// +1, to make it [L,R] from [L,R) 51 | 52 | while( L < R ) { 53 | if( L & 1 ) ret = min( ret , Tree[ L++ ] ); 54 | if( R & 1 ) ret = min( ret , Tree[ --R ] ); 55 | L >>= 1, R >>= 1; 56 | } 57 | 58 | return ret; 59 | } 60 | 61 | }; -------------------------------------------------------------------------------- /Data Structures/Splay Tree.cpp: -------------------------------------------------------------------------------- 1 | template< typename T, typename Comp = std::less< T > > 2 | class SplayTree { 3 | 4 | struct node { 5 | T *element; 6 | node *left, *right, *parent; 7 | node( const T &k ) { 8 | left = right = parent = NULL; 9 | element = new T(k); 10 | } 11 | ~node () { 12 | delete left; 13 | delete right; 14 | delete parent; 15 | delete element; 16 | } 17 | } *root; 18 | 19 | unsigned p_size; 20 | 21 | public : 22 | 23 | SplayTree() { root = NULL; p_size = 0; } 24 | ~SplayTree() { delete root; } 25 | 26 | node *find( T k ) { return find( &root , k ); } 27 | node *find( node **s, T k ) { 28 | if( *s == NULL ) return NULL; 29 | 30 | node *curr, *pred; 31 | 32 | curr = *s; 33 | pred = NULL; 34 | 35 | while( curr ) { 36 | if( k == *( curr -> element ) ) break; 37 | pred = curr; 38 | if( k <= *( curr -> element ) ) curr = curr -> left; 39 | else curr = curr -> right; 40 | } 41 | if( curr ) { 42 | splay( curr ); 43 | return curr ; 44 | } 45 | splay( pred ); 46 | return NULL; 47 | } 48 | 49 | unsigned size() { return p_size; } 50 | 51 | node *insert( T k ) { return insert( &root , k ); } 52 | node *insert( node **s, T k ) { 53 | if( *s == NULL ) { 54 | p_size++; 55 | return *s = new node ( k ); 56 | } 57 | 58 | node *curr = *s; 59 | node *anterior = NULL; 60 | int dir = 0; 61 | while( curr ) { 62 | if( k == *( curr -> element ) ) return curr; 63 | anterior = curr; 64 | if( k <= *( curr -> element ) ) { 65 | dir = -1; 66 | curr = curr -> left; 67 | } 68 | else { 69 | dir = 1; 70 | curr = curr -> right; 71 | } 72 | } 73 | p_size++; 74 | curr = new node( k ); 75 | curr -> parent = anterior; 76 | if( dir == -1 ) anterior -> left = curr ; 77 | else anterior -> right = curr; 78 | splay( curr ); 79 | return curr; 80 | } 81 | 82 | void erase( T k ) { erase( &root , k ); } 83 | void erase( node **root, T k ) { 84 | node *sL, *sR; 85 | if( find( root , k ) ) { 86 | sL = (*root) -> left; 87 | if( sL ) sL -> parent = NULL; 88 | sR = (*root) -> right; 89 | if( sR ) sR -> parent = NULL; 90 | *root = join( sL , sR ); 91 | } 92 | } 93 | 94 | node *join( node *sL , node *sR ) { 95 | if( sL ) { 96 | find( &sL , INT_MAX ); 97 | sL -> right = sR; 98 | if( sR ) sR -> parent = sL; 99 | return sL; 100 | } 101 | else if ( sR ) return sR; 102 | return NULL; 103 | } 104 | 105 | pair< node ** , node ** > split( node *root , T div ) { 106 | if( root ) { 107 | find( &root , div ); 108 | node **sL, **sR; 109 | *sL = root; 110 | *sR = root -> right; 111 | (*sL) -> right = NULL; 112 | return make_pair( sL, sR ); 113 | } 114 | node ** empty; 115 | empty = NULL; 116 | return make_pair( empty , empty ); 117 | } 118 | 119 | void splay( node *crt ) { splay( &root , crt ); } 120 | void splay( node **root , node *crt ) { 121 | node *father = crt -> parent; 122 | while( father ) { 123 | if( father -> parent == NULL ) singleRotate( crt ); 124 | else doubleRotate( crt ); 125 | father = crt -> parent; 126 | } 127 | *root = crt; 128 | } 129 | 130 | void zigLeft( node *x ) { 131 | node *p, *b; 132 | p = x -> parent; 133 | b = x -> right; 134 | x -> right = p; 135 | x -> parent = NULL; 136 | if( b ) b -> parent = p; 137 | p -> left = b; 138 | p -> parent = x; 139 | } 140 | 141 | void zigRight( node *x ) { 142 | node *p, *b; 143 | p = x -> parent; 144 | b = x -> left; 145 | x -> left = p; 146 | x -> parent = NULL; 147 | if( b ) b -> parent = p; 148 | p -> right = b; 149 | p -> parent = x; 150 | } 151 | 152 | void zigZigLeft( node *x ) { 153 | node *p, *g, *ggp; 154 | node *b, *c; 155 | p = x -> parent; 156 | g = p -> parent; 157 | b = x -> right; 158 | c = p -> right; 159 | ggp = g -> parent; 160 | x -> right = p; 161 | p -> parent = x; 162 | p -> right = g; 163 | g -> parent = p; 164 | if( b ) b -> parent = p; 165 | p -> left = b; 166 | if( c ) c -> parent = g; 167 | g -> left = c; 168 | x -> parent = ggp; 169 | if( ggp ) { 170 | if( ggp -> left == g ) ggp -> left = x; 171 | else ggp -> right = x; 172 | } 173 | } 174 | 175 | void zigZigRight ( node *x ) { 176 | node *p, *g, *ggp; 177 | node *b, *c; 178 | p = x -> parent; 179 | g = p -> parent; 180 | b = x -> left; 181 | c = p -> left; 182 | ggp = g -> parent; 183 | x -> left = p; 184 | p -> parent = x; 185 | p -> left = g; 186 | g -> parent = p; 187 | if( b ) b -> parent = p; 188 | p -> right = b; 189 | if( c ) c -> parent = g; 190 | g -> right = c; 191 | x -> parent = ggp; 192 | if( ggp ) { 193 | if( ggp -> left == g ) 194 | ggp -> left = x; 195 | else ggp -> right = x; 196 | } 197 | } 198 | 199 | void zigZagLeft ( node *x ) { 200 | node *p, *g, *ggp; 201 | node *a, *b; 202 | p = x -> parent; 203 | g = p -> parent; 204 | ggp = g -> parent; 205 | a = x -> left; 206 | b = x -> right; 207 | x -> left = g; 208 | g -> parent = x; 209 | x -> right = p; 210 | p -> parent = x; 211 | if( a ) a -> parent = g; 212 | g -> right = a; 213 | if( b ) b -> parent = p; 214 | p -> left = b; 215 | x -> parent = ggp; 216 | if( ggp ) { 217 | if( ggp -> left == g ) 218 | ggp -> left = x; 219 | else ggp -> right = x; 220 | } 221 | } 222 | 223 | void zigZagRight( node *x ) { 224 | node *p, *g, *ggp; 225 | node *a, *b; 226 | 227 | p = x -> parent; 228 | g = p -> parent; 229 | ggp = g -> parent; 230 | a = x -> left; 231 | b = x -> right; 232 | x -> right = g; 233 | g -> parent = x; 234 | x -> left = p; 235 | p -> parent = x; 236 | if( a ) a -> parent = p; 237 | p -> right = a; 238 | if( b ) b -> parent = g; 239 | g -> left = b; 240 | x -> parent = ggp; 241 | if( ggp ) { 242 | if( ggp -> left == g ) ggp -> left = x; 243 | else ggp -> right = x; 244 | } 245 | } 246 | 247 | void singleRotate( node *x ) { 248 | if( x -> parent -> left == x ) zigLeft(x); 249 | else zigRight(x); 250 | } 251 | 252 | void doubleRotate( node *x ) { 253 | if( x -> parent -> left == x && x -> parent -> parent -> left == x -> parent ) 254 | zigZigLeft(x); 255 | else if( x -> parent -> left == x && x -> parent -> parent -> right == x -> parent ) 256 | zigZagLeft(x); 257 | else if( x -> parent -> right == x && x -> parent -> parent -> right == x -> parent ) 258 | zigZigRight(x); 259 | else if( x -> parent -> right == x && x -> parent -> parent -> left == x -> parent ) 260 | zigZagRight(x); 261 | } 262 | }; 263 | -------------------------------------------------------------------------------- /Geometry/Intersections/intersections.cpp: -------------------------------------------------------------------------------- 1 | pair intersectionLineLine( line L1 , line L2 ){ 2 | __typeof(L1.a) det = L1.a * L2.b - L1.b * L2.a; 3 | point ret(-1,-1); 4 | if( !sgn ( det, 0 ) ) return mp(false,ret); 5 | ret.x = ( L1.b * L2.c - L2.b * L1.c ) / det; 6 | ret.y = ( L1.c * L2.a - L2.c * L1.a ) / det; 7 | return mp(true,ret); 8 | } 9 | 10 | pair intersectionLineSegment( line L , segment S ){ 11 | line L2( S.A , S.B ); 12 | point ret(-1,-1); 13 | pair P=intersectionLineLine( L , L2 ); 14 | if(!P.xx) return mp(false,ret); 15 | if( !sgn( dist(P.yy,S.A) + dist(P.yy,S.B) , dist(S.A,S.B) ) ) return mp(true,P.yy); 16 | return mp(false,ret); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Geometry/Others/atan2 Adapter.cpp: -------------------------------------------------------------------------------- 1 | double get(double y,double x){ 2 | if(!(y<-eps)) return atan2(y,x); 3 | x=-x; 4 | double ret=-atan2(y,x); 5 | ret+=pi; 6 | return ret; 7 | } 8 | -------------------------------------------------------------------------------- /Geometry/Polygon related codes/area of a 2D polygon.cpp: -------------------------------------------------------------------------------- 1 | double polygonArea( vector &P ) { 2 | double area = 0; 3 | int n=P.size(); 4 | for( int i = 0, j = n - 1; i < n; j = i++ ) area += P[j].x * P[i].y - P[j].y * P[i].x; 5 | return fabs(area)/2; 6 | } 7 | -------------------------------------------------------------------------------- /Geometry/Polygon related codes/convex hull ( 2D ).cpp: -------------------------------------------------------------------------------- 1 | /* Author : Bidhan Roy 2 | * Complexity : O (Nlog(N)) 3 | * Handles Collinear points and duplicate points 4 | * Report at `mail2bidhan@gmail.com` if you find any buf 5 | */ 6 | 7 | 8 | point p0; 9 | bool comp(point a,point b){ 10 | i64 d=area(p0,a,b); 11 | if(d<0) return false; 12 | if(d) return true; 13 | return sqDist(p0,a) &points,vector &ans){ 16 | int pos=0; 17 | rep(i,sz(points)) 18 | if(points[pos].y>points[i].y || (points[pos].y==points[i].y && points[pos].x>points[i].x)) pos=i; 19 | p0=points[pos]; 20 | sort(all(points),comp); 21 | int i=0; 22 | while(p0==points[i]) { 23 | i++; 24 | if(i==sz(points)) return ; 25 | } 26 | int start=i; 27 | ans.pb(points[i-1]); 28 | while(!area(ans[0],points[start],points[i])) { 29 | i++; 30 | if(i==sz(points)) return ; 31 | } 32 | i--; 33 | int sec=i; 34 | ans.pb(points[i]); bool one=1; 35 | while(!area(ans[0],ans[1],points[i])){ 36 | i++; 37 | if(i==sz(points)) { if(one) return ; else break; } 38 | one=0; 39 | } 40 | if(i-1>sec) i--; 41 | if(i==sz(points)) return ; 42 | ans.pb(points[i]); 43 | i++; 44 | for(; i eps) 45 | dfs(p, f); 46 | else { 47 | add.a = b; 48 | add.b = a; 49 | add.c = p; //Note here that the order to a right-handed 50 | add.ok = true; 51 | g[p][b] = g[a][p] = g[b][a] = num; 52 | F[num++] = add; 53 | } 54 | } 55 | } 56 | void dfs(int p, int now) { 57 | //Recursive search of all should be removed from 58 | //the inner surface of the convex hull 59 | F[now].ok = 0; 60 | deal(p, F[now].b, F[now].a); 61 | deal(p, F[now].c, F[now].b); 62 | deal(p, F[now].a, F[now].c); 63 | } 64 | bool same(int s, int t) { 65 | Point &a = P[F[s].a]; 66 | Point &b = P[F[s].b]; 67 | Point &c = P[F[s].c]; 68 | return fabs(volume(a, b, c, P[F[t].a])) < eps 69 | && fabs(volume(a, b, c, P[F[t].b])) < eps 70 | && fabs(volume(a, b, c, P[F[t].c])) < eps; 71 | } 72 | //Construction of three-dimensional convex hull 73 | void create() { 74 | int i, j, tmp; 75 | face add; 76 | num = 0; 77 | if (n < 4) return; 78 | //********************************************** 79 | //This section is to ensure that the first four non-coplanar points 80 | bool flag = true; 81 | for (i = 1; i < n; i++) { 82 | if (vlen(P[0] - P[i]) > eps) { 83 | swap(P[1], P[i]); 84 | flag = false; 85 | break; 86 | } 87 | } 88 | if (flag) return; 89 | flag = true; 90 | //So that the first three points are not collinear 91 | for (i = 2; i < n; i++) { 92 | if (vlen((P[0] - P[1]) * (P[1] - P[i])) > eps) { 93 | swap(P[2], P[i]); 94 | flag = false; 95 | break; 96 | } 97 | } 98 | if (flag) return; 99 | flag = true; 100 | //Not four points of the front face 101 | for (int i = 3; i < n; i++) { 102 | if (fabs((P[0] - P[1]) * (P[1] - P[2]) ^ (P[0] - P[i])) > eps) { 103 | swap(P[3], P[i]); 104 | flag = false; 105 | break; 106 | } 107 | } 108 | if (flag) return; 109 | //***************************************** 110 | for (i = 0; i < 4; i++) { 111 | add.a = (i + 1) % 4; 112 | add.b = (i + 2) % 4; 113 | add.c = (i + 3) % 4; 114 | add.ok = true; 115 | if (dblcmp(P[i], add) > 0) 116 | swap(add.b, add.c); 117 | g[add.a][add.b] = g[add.b][add.c] = g[add.c][add.a] = num; 118 | F[num++] = add; 119 | } 120 | for (i = 4; i < n; i++) { 121 | for (j = 0; j < num; j++) { 122 | if (F[j].ok && dblcmp(P[i], F[j]) > eps) { 123 | dfs(i, j); 124 | break; 125 | } 126 | } 127 | } 128 | tmp = num; 129 | for (i = num = 0; i < tmp; i++) 130 | if (F[i].ok) 131 | F[num++] = F[i]; 132 | } 133 | //Surface 134 | double area() { 135 | double res = 0; 136 | if (n == 3) { 137 | Point p = cross(P[0], P[1], P[2]); 138 | res = vlen(p) / 2.0; 139 | return res; 140 | } 141 | for (int i = 0; i < num; i++) 142 | res += area(P[F[i].a], P[F[i].b], P[F[i].c]); 143 | return res / 2.0; 144 | } 145 | double volume() { 146 | double res = 0; 147 | Point tmp(0, 0, 0); 148 | for (int i = 0; i < num; i++) 149 | res += volume(tmp, P[F[i].a], P[F[i].b], P[F[i].c]); 150 | return fabs(res / 6.0); 151 | } 152 | //The number of surface triangles 153 | int triangle() { return num; } 154 | //The number of polygons surface 155 | int polygon() { 156 | int i, j, res, flag; 157 | for (i = res = 0; i < num; i++) { 158 | flag = 1; 159 | for (j = 0; j < i; j++) 160 | if (same(i, j)) { 161 | flag = 0; 162 | break; 163 | } 164 | res += flag; 165 | } 166 | return res; 167 | } 168 | //Three-dimensional convex hull focus 169 | Point barycenter() { 170 | Point ans(0, 0, 0), o(0, 0, 0); 171 | double all = 0; 172 | for (int i = 0; i < num; i++) { 173 | double vol = volume(o, P[F[i].a], P[F[i].b], P[F[i].c]); 174 | ans = ans + (o + P[F[i].a] + P[F[i].b] + P[F[i].c]) / 4.0 * vol; 175 | all += vol; 176 | } 177 | ans = ans / all; 178 | return ans; 179 | } 180 | //Point to the plane distance 181 | double ptoface(Point p, int i) { 182 | return fabs( volume(P[F[i].a], P[F[i].b], P[F[i].c], p) 183 | / vlen((P[F[i].b] - P[F[i].a]) * (P[F[i].c] - P[F[i].a]))); 184 | } 185 | #undef MAXN 186 | }; 187 | -------------------------------------------------------------------------------- /Geometry/Structures & classes used in my code/line.cpp: -------------------------------------------------------------------------------- 1 | struct line{ 2 | #define DT int /// Datatype 3 | DT a,b,c; 4 | 5 | line(){a=b=c=0;} /// default constructor 6 | line(point p,point q):a(p.y-q.y),b(q.x-p.x),c(p.x*q.y-q.x*p.y){} /// constructor 7 | 8 | bool operator < (line B); /// smaller than operator 9 | 10 | void normalize(); /// generalizes the line coefficients 'a','b','c' 11 | line perpendicular(point p); 12 | /// returns the perpendicular line 13 | /// which goes through point 'p' 14 | }; 15 | 16 | void line::normalize(){ 17 | /// generalizes the line coefficients 'a','b','c' 18 | __typeof(a) g=gcd(a,gcd(b,c)); 19 | a/=g, b/=g, c/=g; 20 | int sign=(a<0 || (!a && b<0))?-1:1; 21 | a*=sign, b*=sign, c*=sign; 22 | } 23 | 24 | line line::perpendicular(point p){ 25 | /// returns the perpendicular line 26 | /// which goes through point 'p' 27 | line ret; 28 | ret.a=b, ret.b=-a; 29 | ret.c=-p.x*ret.a-p.y*ret.b; 30 | return ret; 31 | } 32 | 33 | bool line::operator < (line B) { 34 | /// smaller than operator 35 | if(!sgn(a,B.a)) { 36 | return sgn(b,B.b)==0?sgn(c,B.c)<0:sgn(b,B.b)<0; 37 | } 38 | return sgn(a,B.a)<0; 39 | } 40 | -------------------------------------------------------------------------------- /Geometry/Structures & classes used in my code/point .cpp: -------------------------------------------------------------------------------- 1 | struct point{ 2 | #define DT int /// Datatype 3 | DT x,y; 4 | point():x(0),y(0){} /// default constructor 5 | point(DT _x,DT _y):x(_x),y(_y){} /// constructor 6 | 7 | bool operator < (point b) ; /// smaller than operator 8 | bool operator == (point b) ; /// equal to operator 9 | point operator - (point b) ; /// returns a vector from `this` to `b` 10 | DT operator * (point b) ; /// Dot product of `this` and `b` 11 | DT operator ^ (point b) ; /// Cross product of `this` and `b` 12 | point pointBetween(point q,double m1,double m2); 13 | /// returns a point from the joining segment of 'this' and 'q' 14 | /// the distance ratio of which from 'this' and 'q' is m1:m2 15 | }; 16 | 17 | bool point::operator < (point b) { 18 | /// smaller than operator 19 | if(!sgn(x,b.x)) return sgn(y,b.y)<0; 20 | return sgn(x,b.x)<0; 21 | } 22 | 23 | bool point::operator == (point b) { 24 | /// equal to operator 25 | return !sgn(x,b.x) && !sgn(y,b.y); 26 | } 27 | 28 | point point::operator - (point b){ 29 | /// returns a vector from `this` to `b` 30 | return point(x-b.x,y-b.y); 31 | } 32 | 33 | __typeof(point::x) point::operator * (point b){ 34 | /// Dot product 35 | return x*b.x+y*b.y; 36 | } 37 | 38 | __typeof(point::x) point::operator ^ (point b){ 39 | /// Cross product 40 | return x*b.y-y*b.x; 41 | } 42 | 43 | point point::pointBetween(point q,double m1,double m2){ 44 | /// returns a point from the joining segment of 'this' and 'q' 45 | /// the distance ratio of which from 'this' and 'q' is m1:m2 46 | return point( ( m1 * q.x + m2 * x ) / ( m1 + m2 ) , ( m1 * q.y + m2 * y ) / ( m1 + m2 ) ); 47 | } 48 | -------------------------------------------------------------------------------- /Geometry/Structures & classes used in my code/point(3D).cpp: -------------------------------------------------------------------------------- 1 | struct Point { 2 | double x, y, z; 3 | Point() {} 4 | 5 | Point(double xx, double yy, double zz) : x(xx), y(yy), z(zz) {} 6 | 7 | //The difference between the two vectors 8 | Point operator -(const Point p1) { 9 | return Point(x - p1.x, y - p1.y, z - p1.z); 10 | } 11 | 12 | //The sum of two vectors 13 | Point operator +(const Point p1) { 14 | return Point(x + p1.x, y + p1.y, z + p1.z); 15 | } 16 | 17 | //cross 18 | Point operator *(const Point p) { 19 | return Point(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x); 20 | } 21 | 22 | Point operator *(double d) { 23 | return Point(x * d, y * d, z * d); 24 | } 25 | 26 | Point operator /(double d) { 27 | return Point(x / d, y / d, z / d); 28 | } 29 | 30 | //Dot 31 | double operator ^(Point p) { 32 | return (x * p.x + y * p.y + z * p.z); 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Geometry/Structures & classes used in my code/segment.cpp: -------------------------------------------------------------------------------- 1 | struct segment{ 2 | point A,B; 3 | segment():A(point(0,0)),B(point(0,0)){} 4 | segment(point _A,point _B):A(_A),B(_B){} 5 | bool inside(point c); /// checks if a point is inside the enclosing rectangle 6 | bool intersect(segment Q) ; /// checks if 'this' intersect with 'Q' 7 | }; 8 | 9 | bool segment::inside(point c){ 10 | /// checks if a point is inside the enclosing rectangle 11 | return (min(A.x,B.x)<=c.x && c.x<=max(A.x,B.x) && min(A.y,B.y)<=c.y && c.y<=max(A.y,B.y)); 12 | } 13 | 14 | bool segment::intersect(segment Q){ 15 | /// checks if 'this' intersect with 'Q' 16 | __typeof(A.x) d1=area(Q.A,Q.B,A); 17 | __typeof(A.x) d2=area(Q.A,Q.B,B); 18 | __typeof(A.x) d3=area(A,B,Q.A); 19 | __typeof(A.x) d4=area(A,B,Q.B); 20 | if( ((d1>0 && d2<0) || (d1<0 && d2>0)) && ((d3>0 && d4<0) || (d3<0 && d4>0)) ) return true; 21 | if(!d1 && Q.inside(A)) return true; 22 | if(!d2 && Q.inside(B)) return true; 23 | if(!d3 && inside(Q.A)) return true; 24 | if(!d4 && inside(Q.B)) return true; 25 | return false; 26 | } 27 | -------------------------------------------------------------------------------- /Geometry/geometry template extension.cpp: -------------------------------------------------------------------------------- 1 | const double pi=M_PI; 2 | #define area(a,b,c) ((b-a)^(c-a)) 3 | #define sqDist(a,b) (sqr(a.x-b.x)+sqr(a.y-b.y)) 4 | #define dist(a,b) sqrt(sqDist(a,b)) 5 | -------------------------------------------------------------------------------- /Graph Algorithms & Data Structures/Bipartite Matching ( Hopcroft Karp ).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Author : Bidhan Roy 3 | * Required Headers : 4 | * Complexity : O (|E|√|V|) 5 | * 1 based indexing 6 | * If you find any bug contact me : mail2bidhan@gmail.com 7 | */ 8 | 9 | namespace hopcroftKarp{ 10 | #define MAXN 100001 /// Maximum possible Number of nodes 11 | #define MAXE 150001 /// Maximum possible Number of edges 12 | #define INF (1<<29) 13 | int ptr[MAXN],next[MAXE],zu[MAXE]; 14 | int n,m,match[MAXN],D[MAXN],q[MAXN]; 15 | void init(int _n){ /// initialization _n=number of nodes 16 | n=_n; 17 | m=0; 18 | memset(ptr,~0,sizeof(int)*(n+1)); 19 | } 20 | void add_edge(int u,int v){ /// Adding edge between u and v 21 | next[m]=ptr[u];ptr[u]=m;zu[m]=v;++m; 22 | } 23 | bool bfs(){ 24 | int u,v; 25 | register int i; 26 | int qh=0, qt=0; 27 | for(i=1; i<=n; i++){ 28 | if(!match[i]){ 29 | D[i]=0; 30 | q[qt++]=i; 31 | } 32 | else D[i]=INF; 33 | } 34 | D[0]=INF; 35 | while(qh g[MAXN]; 18 | 19 | void init(int _n,int _s,int _t){ 20 | n=_n, s=_s, t=_t; 21 | esize=0; 22 | //for(int i=0; iwEPS){ 39 | if(pot[zu[i]]>pot[u]+cost[i]+cEPS){ 40 | pot[zu[i]]=pot[u]+cost[i]; cont=1; 41 | } 42 | } 43 | } 44 | 45 | for(toc=0,tof=0;tof+wEPSnode; 47 | priority_queue,greater > q; 48 | for(u=0;uwEPS){ 53 | cc=c+cost[i]+pot[u]-pot[v=zu[i]]; 54 | if(d[v]>cc){q.push(mp(d[v]=cc,v));pree[v]=i;} 55 | } 56 | } 57 | if(!vis[ink])return 0; 58 | f=flo-tof; 59 | for(v=ink;v!=src;v=zu[i^1]) {i=pree[v];f=min(f,capa[i]);} 60 | for(v=ink;v!=src;v=zu[i^1]) {i=pree[v];capa[i]-=f;capa[i^1]+=f;} 61 | tof+=f; 62 | toc+=f*(d[ink]-pot[src]+pot[ink]); 63 | for(u=0;u, 4 | * Complexity : O(N) 5 | * If you find any bug report me : mail2bidhan@gmail.com 6 | */ 7 | 8 | #define mx 100000 ///Maximum possible number of nodes 9 | 10 | vector edge[mx]; 11 | int _low[mx], _dtime[mx], _dfstime, _stack[mx], _size, _comp[mx], _comps; 12 | bool _instack[mx]; 13 | 14 | void tarjan(int u){ 15 | _low[u]=_dtime[u]=++_dfstime; 16 | _stack[_size++]=u; 17 | _instack[u]=true; 18 | int i; 19 | for(i=0; i< int(edge[u].size()); i++){ 20 | int v=edge[u][i]; 21 | if(_dtime[v]==-1) tarjan(v), _low[u]=min(_low[u],_low[v]); 22 | else if(_instack[v]) _low[u]=min(_low[u],_dtime[v]); 23 | } 24 | 25 | if(_dtime[u]==_low[u]){ 26 | _comps++; 27 | int v=-1; 28 | do{ 29 | v=_stack[--_size]; 30 | _instack[v]=false; 31 | _comp[v]=_comps; 32 | }while(u!=v); 33 | } 34 | } 35 | 36 | void scc(int n){ 37 | _comps=_dfstime=_size=0; 38 | memset(_dtime,-1,(n+1)*sizeof(int)); 39 | memset(_low,0,(n+1)*sizeof(int)); 40 | memset(_stack,0,(n+1)*sizeof(int)); 41 | memset(_comp,0,(n+1)*sizeof(int)); 42 | memset(_instack,0,(n+1)*sizeof(int)); 43 | int i; 44 | for(i=0; i>=1){ 6 | res*=res; 7 | if(m>0) res%= m; 8 | if(p&i) { 9 | res*=b; 10 | if(m>0) res%=m; 11 | } 12 | } 13 | return res; 14 | } 15 | -------------------------------------------------------------------------------- /Mathematics/Chinese Remainder Theorem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Author : Bidhan Roy 3 | * Required Headers : 4 | * If you find any bug report at : mail2bidhan@gmail.com 5 | * Solves equations of the format x % mods[i] = r[i], ( 0<=i &r,const vector< long long > &mods){ 9 | long long M=1; 10 | for(int i=0; i m, s; 12 | for(int i=0; i=M) ret-=M; 30 | } 31 | return ret; 32 | } 33 | -------------------------------------------------------------------------------- /Mathematics/Matrix Exponentiation.cpp: -------------------------------------------------------------------------------- 1 | namespace matrix{ 2 | #define size 105 ///Maximum size of the matrix 3 | #define wint int ///datatype to use (int,long long etc) 4 | wint mat[size][size],tmp[size][size],res[size][size]; 5 | wint MOD; 6 | int n; 7 | void init(int _n,wint _MOD){ ///initialization, _n=size of the square matrix , _MOD=mod value 8 | n=_n; 9 | MOD=_MOD; 10 | memset(mat,0,sizeof (mat)); 11 | memset(tmp,0,sizeof (tmp)); 12 | memset(res,0,sizeof (res)); 13 | } 14 | void mul(wint r[][size],wint a[][size],wint b[][size]) { 15 | int i,j,t; 16 | for(i=0; i0) { 29 | if(p&1) mul(r,r,a); 30 | mul(a,a,a); p>>=1; 31 | } 32 | } 33 | void pow(wint p){ 34 | mPow(res,mat,p); 35 | memcpy(mat,res,n*size*sizeof(tmp[0][0])); 36 | } 37 | void print(wint pp[][size]){ 38 | int i,j; 39 | for(i=0; i'9') && (c<'a' || c>'z') && (c<'A' || c>'Z') ) continue; 20 | break; 21 | } 22 | 23 | str[ Len++ ] = c; 24 | 25 | while(1) { 26 | c = getchar_unlocked(); 27 | if( (c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') ) str[ Len++ ] = c; 28 | else break; 29 | } 30 | 31 | str[ Len ] = '\0'; 32 | 33 | return Len; 34 | } 35 | 36 | long long getLongLong(){ 37 | 38 | long long res = 0; 39 | char c; 40 | bool negative = false; 41 | 42 | while(1) { 43 | c = getchar_unlocked(); 44 | if( (c<'0' || c>'9') && c!='-') continue; 45 | break; 46 | } 47 | 48 | if( c=='-' ) negative = true, c = getchar_unlocked(); 49 | 50 | res = c-'0'; 51 | 52 | while(1) { 53 | c = getchar_unlocked(); 54 | if( c>='0' && c<='9' ) res = 10ll*res+c-'0'; 55 | else break; 56 | } 57 | 58 | return negative?-res:res; 59 | } 60 | 61 | int getInt(){ 62 | 63 | int res = 0; 64 | char c; 65 | bool negative = false; 66 | 67 | while(1) { 68 | c = getchar_unlocked(); 69 | if( (c<'0' || c>'9') && c!='-') continue; 70 | break; 71 | } 72 | 73 | if( c=='-' ) negative = true, c = getchar_unlocked(); 74 | 75 | res = c-'0'; 76 | 77 | while(1) { 78 | c = getchar_unlocked(); 79 | if( c>='0' && c<='9' ) res = 10*res+c-'0'; 80 | else break; 81 | } 82 | 83 | return negative?-res:res; 84 | } 85 | 86 | }; 87 | -------------------------------------------------------------------------------- /Miscellaneous Codes/Roman - Decimal Converter ( Both way ).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Author : Bidhan Roy 3 | * Required Headers : 4 | * Range 0 to 4999 . 0 euals to empty string ("") 5 | * Roman numerals are expressed in uppercase 6 | * If you find any bug contact me : mail2bidhan@gmail.com 7 | */ 8 | 9 | string decimalToRoman( int n ){ 10 | if(n<4) return string(n,'I'); 11 | if(n<6) return string(5-n,'I')+"V"; 12 | if(n<9) return "V"+string(n-5,'I'); 13 | if(n<11) return string(10-n,'I')+"X"; 14 | if(n<40) return string(n/10,'X')+decimalToRoman(n%10); 15 | if(n<60) return string(5-n/10,'X')+'L'+decimalToRoman(n%10); 16 | if(n<90) return "L"+string(n/10-5,'X')+decimalToRoman(n%10); 17 | if(n<110) return string(10-n/10,'X')+"C"+decimalToRoman(n%10); 18 | if(n<400) return string(n/100,'C')+decimalToRoman(n%100); 19 | if(n<600) return string(5-n/100,'C')+'D'+decimalToRoman(n%100); 20 | if(n<900) return "D"+string(n/100-5,'C')+decimalToRoman(n%100); 21 | if(n<1100) return string(10-n/100,'C')+"M"+decimalToRoman(n%100); 22 | return string(n/1000,'M')+decimalToRoman(n%1000); 23 | } 24 | 25 | int romanToDecimal(string str){ 26 | if(str=="") return 0; 27 | int res=0,j,m=0; 28 | string :: iterator p=str.end(); 29 | const char *q; 30 | for(--p;;p--) { 31 | for(q="IVXLCDM",j=0;*q;q++,j++) if(*p==*q) 32 | res+=((j>=m)?m=j,1:-1)*(1+j%4/2*9)*(1+j/4*99)*(1+j%2*4); 33 | if(p==str.begin()) break; 34 | } 35 | return res; 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Algorithm-Code-Library 2 | ====================== 3 | 4 | Library of my implementations of various algorithm and data-structures. Used by me for online contests. Written in C++. 5 | 6 | If you find any of the codes useful, I would love to hear from you. 7 | 8 | Email: mail2bidhan@gmail.com 9 | -------------------------------------------------------------------------------- /Sample Codes/Knuth Optimization ( Uva 10304 ).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Bidhan Roy 3 | * University of Dhaka 4 | * Problem : Uva - 10304 Optimal Binary Search Tree 5 | */ 6 | 7 | #include 8 | 9 | #define mx 260 10 | #define inf 1<<28 11 | 12 | int a[mx]; 13 | 14 | int dp[mx][mx]; /// contains the optimal costs of intervals 15 | int p[mx][mx]; /// contains the points by partitioning where we can get minimum cost 16 | int sum[mx]; /// contains cumulative sum of the array 17 | 18 | int main(){ 19 | int n; 20 | while( scanf("%d",&n)==1 ){ 21 | for(int i=0; i0) i=F[i]; 23 | else j++; 24 | } 25 | return ret; 26 | } 27 | -------------------------------------------------------------------------------- /String Algorithms & Data Structures/Manacher.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Author : Bidhan Roy 3 | * Required Headers : ,; 4 | * Complexity : O (|N|) 5 | * If you find any bug report at : mail2bidhan@gmail.com 6 | */ 7 | 8 | int call(char *inp,char *str,int *F,vector< pair > &vec){ 9 | //inp is the actual string 10 | //str is the modified string with double size of inp 11 | //F[i] contains the length of the palindrome centered at index i 12 | //Every element of vec cointains starting and ending positions of palindromes 13 | int len=0; 14 | str[len++]='*'; 15 | for(int i=0; inp[i]; i++){ 16 | str[len++]=inp[i]; 17 | str[len++]='*'; 18 | } 19 | str[len]='\0'; 20 | int c=0,r=0,ans=0; 21 | for(int i=1; i < len-1; i++){ 22 | int _i=c-(i-c); 23 | if(r > i) F[i]=min(F[_i],r-i); 24 | else F[i]=0; 25 | while(i-1-F[i]>=0 && str[i-1-F[i]]==str[i+1+F[i]]) { 26 | F[i]++; 27 | } 28 | if(i+F[i] > r) r=i+F[i],c=i; 29 | ans=max(ans,F[i]); 30 | vec.push_back(make_pair(i-F[i],i+F[i])); 31 | } 32 | return ans; 33 | } 34 | -------------------------------------------------------------------------------- /String Algorithms & Data Structures/Minimum Lexicographical Rotation O ( N ) .cpp: -------------------------------------------------------------------------------- 1 | int minmove(char *inp,int len){ 2 | int ret=0,l=0,idx=1; 3 | while(ret+l+1 tokenize( string a, string b ) { 2 | const char *q = a.c_str(); 3 | while( count( b.begin(), b.end(), *q ) ) q++; 4 | vector< string > oot; 5 | while( *q ) { 6 | const char *e = q; 7 | while( *e && !count( b.begin(), b.end(), *e ) ) e++; 8 | oot.push_back( string( q, e ) ); 9 | q = e; 10 | while( count( b.begin(), b.end(), *q ) ) q++; 11 | } 12 | return oot; 13 | } 14 | -------------------------------------------------------------------------------- /String Algorithms & Data Structures/Suffix Array.cpp: -------------------------------------------------------------------------------- 1 | namespace suffixArray{ 2 | const int MAXN = 2005; /// Length of string 3 | const int MAXL = 22; 4 | int n ,stp,mv,suffix[MAXN],tmp[MAXN]; 5 | int sum[MAXN],cnt[MAXN],rank[MAXL][MAXN]; 6 | char str[MAXN]; 7 | int LCP(int u,int v){ 8 | int ret=0,i; 9 | for(i = stp; i >= 0; i--){ 10 | if(rank[i][u]==rank[i][v]){ 11 | ret += 1< backLink; 11 | 12 | bool has ( char ch ) { 13 | return next[ charToInt(ch) ] != -1; 14 | } 15 | }; 16 | 17 | int sz, last; 18 | vector< state > Pool; 19 | 20 | void getAllOccurrence ( int v , int P_length , vector &ret ) { 21 | if ( !Pool[v].isClone ) ret.pb( Pool[v].firstPos - P_length + 1 ); 22 | for ( size_t i = 0; i < Pool[v].backLink.size(); i++ ) 23 | getAllOccurrence ( Pool[v].backLink[i] , P_length , ret); 24 | } 25 | 26 | public : 27 | 28 | SuffixAutomata( int maxlength ) { 29 | Pool = vector ( maxlength * 2 ); 30 | } 31 | 32 | void initialize() { 33 | rep( i , sz ) Pool[i].backLink.clr, Pool[i].isClone = false, Pool[i].link = -1, Pool[i].len = 0; 34 | sz = last = 0; 35 | Pool[0].len = 0; 36 | Pool[0].link = -1; 37 | mem( Pool[sz].next , -1 ); 38 | ++sz; 39 | } 40 | 41 | void addChar ( char c ) { 42 | int cur = sz++; 43 | mem( Pool[cur].next , -1 ); 44 | Pool[cur].len = Pool[last].len + 1; 45 | Pool[cur].firstPos = Pool[cur].len - 1; 46 | Pool[cur].isClone = false; 47 | int p; 48 | for ( p = last; p != -1 && !Pool[p].has(c) ; p = Pool[p].link ) Pool[p].next[ charToInt(c) ] = cur; 49 | if ( p == -1 ) Pool[cur].link = 0; 50 | else { 51 | int q = Pool[p].next[ charToInt(c) ]; 52 | if ( Pool[p].len + 1 == Pool[q].len ) Pool[cur].link = q; 53 | else { 54 | int clone = sz++; 55 | mem( Pool[clone].next , -1 ); 56 | Pool[clone].len = Pool[p].len + 1; 57 | Pool[clone].firstPos = Pool[p].firstPos; 58 | Pool[clone].isClone = true; 59 | memcpy( Pool[clone].next , Pool[q].next , sizeof (Pool[q].next) ); 60 | Pool[clone].link = Pool[q].link; 61 | for ( ; p != -1 && Pool[p].next[charToInt(c)] == q; p = Pool[p].link ) 62 | Pool[p].next[ charToInt(c) ] = clone; 63 | Pool[q].link = Pool[cur].link = clone; 64 | } 65 | } 66 | last = cur; 67 | } 68 | 69 | void construct ( string &str ) { 70 | rep( i , sz(str) ) addChar(str[i]); 71 | for( int v=1; v getAllOccurrence ( string &pattern ) { 94 | vector ret; 95 | int v = findState( pattern ); 96 | if( v == -1 ) return ret; 97 | getAllOccurrence( v , sz(pattern) , ret ); 98 | sort ( all ( ret ) ); 99 | return ret; 100 | } 101 | 102 | #undef charToInt 103 | }; 104 | -------------------------------------------------------------------------------- /String Algorithms & Data Structures/Trie.cpp: -------------------------------------------------------------------------------- 1 | struct Trie{ 2 | Trie *next[alphabet+1]; 3 | 4 | Trie(){ for(int i=0; i<=alphabet; i++) next[i]=NULL; } 5 | ~Trie(){ for(int i=0; i<=alphabet; i++) delete(next[i]); } 6 | 7 | void insert(char *str){ 8 | /// inserting character array 9 | Trie *curr=this; 10 | for(int i=0; str[i]; i++){ 11 | if(!curr->next[str[i]-'a']) 12 | curr->next[str[i]-'a']=new Trie; 13 | curr=curr->next[str[i]-'a']; 14 | /// assuming, all the letters are in lowercase, 15 | /// needs to be changed if otherwise 16 | } 17 | if(!curr->next[alphabet]) curr->next[alphabet]=new Trie; 18 | } 19 | 20 | bool find(char *str){ 21 | /// returns 'true' is string 'str' has been inserted in the trie 22 | /// returns 'false' otherwise 23 | Trie *curr=this; 24 | for(int i=0; str[i]; i++){ 25 | if(!curr->next[str[i]-'a']) return false; 26 | curr=curr->next[str[i]-'a']; 27 | } 28 | return curr->next[alphabet]; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Templates/Debug Extensions.cpp: -------------------------------------------------------------------------------- 1 | /// ********* debug template by Bidhan Roy ********* 2 | 3 | template < typename F, typename S > 4 | ostream& operator << ( ostream& os, const pair< F, S > & p ) { 5 | return os << "(" << p.first << ", " << p.second << ")"; 6 | } 7 | 8 | template < typename T > 9 | ostream &operator << ( ostream & os, const vector< T > &v ) { 10 | os << "{"; 11 | typename vector< T > :: const_iterator it; 12 | for( it = v.begin(); it != v.end(); it++ ) { 13 | if( it != v.begin() ) os << ", "; 14 | os << *it; 15 | } 16 | return os << "}"; 17 | } 18 | 19 | template < typename T > 20 | ostream &operator << ( ostream & os, const set< T > &v ) { 21 | os << "["; 22 | typename set< T > :: const_iterator it; 23 | for ( it = v.begin(); it != v.end(); it++ ) { 24 | if( it != v.begin() ) os << ", "; 25 | os << *it; 26 | } 27 | return os << "]"; 28 | } 29 | 30 | template < typename F, typename S > 31 | ostream &operator << ( ostream & os, const map< F, S > &v ) { 32 | os << "["; 33 | typename map< F , S >::const_iterator it; 34 | for( it = v.begin(); it != v.end(); it++ ) { 35 | if( it != v.begin() ) os << ", "; 36 | os << it -> first << " = " << it -> second ; 37 | } 38 | return os << "]"; 39 | } 40 | 41 | #define deb(x) cerr << #x << " = " << x << endl; 42 | -------------------------------------------------------------------------------- /Templates/google codejam template.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Bidhan Roy 3 | * University of Dhaka 4 | */ 5 | 6 | using namespace std; 7 | #include 8 | 9 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 10 | #define sgn(x,y) ((x)+eps<(y)?-1:((x)>eps+(y)?1:0)) 11 | #define rep(i,n) for(__typeof(n) i=0; i<(n); i++) 12 | #define mem(x,val) memset((x),(val),sizeof(x)); 13 | #define rite(x) freopen(x,"w",stdout); 14 | #define read(x) freopen(x,"r",stdin); 15 | #define all(x) x.begin(),x.end() 16 | #define sz(x) ((int)x.size()) 17 | #define sqr(x) ((x)*(x)) 18 | #define pb push_back 19 | #define mp make_pair 20 | #define clr clear() 21 | #define inf (1<<30) 22 | #define ins insert 23 | #define xx first 24 | #define yy second 25 | #define eps 1e-9 26 | 27 | typedef long long i64; 28 | typedef unsigned long long ui64; 29 | typedef string st; 30 | typedef vector vi; 31 | typedef vector vs; 32 | typedef map mii; 33 | typedef map msi; 34 | typedef set si; 35 | typedef set ss; 36 | typedef pair pii; 37 | typedef vector vpii; 38 | 39 | #define mx 0 40 | 41 | int main(){ 42 | time_t start=clock(); 43 | //read("in.txt"); 44 | //rite("out.txt"); 45 | ios_base::sync_with_stdio(0); 46 | int test, kas=0; 47 | cin>>test; 48 | while( test-- ){ 49 | cout<<"Case #"<<++kas<<": "; 50 | } 51 | cerr << " Program has run "<< ( clock()-start ) / CLOCKS_PER_SEC << " s " << endl; 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Templates/online contest template (C++11).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Bidhan Roy 3 | * University of Dhaka 4 | */ 5 | 6 | using namespace std; 7 | #include 8 | 9 | #define rep(i,n) for(auto i=0; i<(n); i++) 10 | #define mem(x,val) memset((x),(val),sizeof(x)); 11 | #define rite(x) freopen(x,"w",stdout); 12 | #define read(x) freopen(x,"r",stdin); 13 | #define all(x) x.begin(),x.end() 14 | #define sz(x) ((int)x.size()) 15 | #define sqr(x) ((x)*(x)) 16 | #define pb push_back 17 | #define clr clear() 18 | #define inf (1<<30) 19 | #define ins insert 20 | #define xx first 21 | #define yy second 22 | #define eps 1e-9 23 | 24 | typedef long long i64; 25 | typedef unsigned long long ui64; 26 | typedef string st; 27 | typedef vector vi; 28 | typedef vector vs; 29 | typedef map mii; 30 | typedef map msi; 31 | typedef set si; 32 | typedef set ss; 33 | typedef pair pii; 34 | typedef vector vpii; 35 | 36 | #define mx 0 37 | 38 | int main() { 39 | ios_base::sync_with_stdio(0); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Templates/online contest template.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Bidhan Roy 3 | * University of Dhaka 4 | */ 5 | 6 | using namespace std; 7 | #include 8 | 9 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 10 | #define sgn(x,y) ((x)+eps<(y)?-1:((x)>eps+(y)?1:0)) 11 | #define rep(i,n) for(__typeof(n) i=0; i<(n); i++) 12 | #define mem(x,val) memset((x),(val),sizeof(x)); 13 | #define rite(x) freopen(x,"w",stdout); 14 | #define read(x) freopen(x,"r",stdin); 15 | #define all(x) x.begin(),x.end() 16 | #define sz(x) ((int)x.size()) 17 | #define sqr(x) ((x)*(x)) 18 | #define pb push_back 19 | #define mp make_pair 20 | #define clr clear() 21 | #define inf (1<<30) 22 | #define ins insert 23 | #define xx first 24 | #define yy second 25 | #define eps 1e-9 26 | 27 | typedef long long i64; 28 | typedef unsigned long long ui64; 29 | typedef string st; 30 | typedef vector vi; 31 | typedef vector vs; 32 | typedef map mii; 33 | typedef map msi; 34 | typedef set si; 35 | typedef set ss; 36 | typedef pair pii; 37 | typedef vector vpii; 38 | 39 | #define mx 0 40 | 41 | int main(){ 42 | ios_base::sync_with_stdio(0); 43 | return 0; 44 | } 45 | --------------------------------------------------------------------------------