diff --git a/Graph Algorithm/Bellmann_Ford.cpp b/Graph Algorithm/Bellmann_Ford.cpp new file mode 100644 index 0000000..c2cff73 --- /dev/null +++ b/Graph Algorithm/Bellmann_Ford.cpp @@ -0,0 +1,19 @@ +vector dist(n+1,(int)-1e17); + +dist[1]=0; + +for(int i = 1 ; i < n ; i++ ){ + for(auto& e : g ){ + int a,b,w; + tie(a,b,w)=e; + dist[b]=max(dist[b],dist[a]+w); + } +} + +for(auto& e : g ){ + int a,b,w; + tie(a,b,w)=e; + if( dist[b] < dist[a] + w ){ + cycle[b]=true; + } +} \ No newline at end of file diff --git a/Graph Algorithm/Flloyd_Warshall.cpp b/Graph Algorithm/Flloyd_Warshall.cpp new file mode 100644 index 0000000..b3a9b5c --- /dev/null +++ b/Graph Algorithm/Flloyd_Warshall.cpp @@ -0,0 +1,7 @@ +for(int k = 1 ; k <= n ; k++ ){ + for(int i = 1 ; i <= n ; i++ ){ + for(int j = 1 ; j <= n ; j++ ){ + dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]); + } + } +} \ No newline at end of file diff --git a/Graph Algorithm/dijikstra.cpp b/Graph Algorithm/dijikstra.cpp new file mode 100644 index 0000000..735b0e7 --- /dev/null +++ b/Graph Algorithm/dijikstra.cpp @@ -0,0 +1,17 @@ +priority_queue q; +q.push({0,1}); +dist[1]=0; +while(!q.empty()){ + pii n=q.top();q.pop(); + int weight=n.F,node=n.S; + + if( vis[node] )continue; + vis[node]=true; + for(pii& nbr : g[node]){ + int n_nbr=nbr.F,n_w=nbr.S; + if( dist[node]+n_w>=1; + } + return res; +} \ No newline at end of file diff --git a/Number Theory/extended_gcd_recursive.cpp b/Number Theory/extended_gcd_recursive.cpp new file mode 100644 index 0000000..e955516 --- /dev/null +++ b/Number Theory/extended_gcd_recursive.cpp @@ -0,0 +1,12 @@ +//Extended gcd -> Recursive + +tuple extended_gcd( int a, int b){ + + if( b == 0 ){ + return {1,0,a}; + } + + int x,y,g; + tie(x,y,g) = extended_gcd( b , a%b ); + return {y , x - (a/b)*y , g}; +} \ No newline at end of file diff --git a/Number Theory/inverse_modulo_array.cpp b/Number Theory/inverse_modulo_array.cpp new file mode 100644 index 0000000..aaf1bef --- /dev/null +++ b/Number Theory/inverse_modulo_array.cpp @@ -0,0 +1,24 @@ +vector invs( vi& a , int m ){ + int n = (int)a.size(); + + if( n == 0 ) return {}; + + vector b(n); + + int v = 1; + for(int i = 0 ; i < n ; i++ ){ + b[i] = v; + v = ((long long)v * a[i] ) % m; + } + + int x = power( v , m - 2 , m ) ; + + x = (x % m + m ) % m; + + for(int i = n - 1 ; i >= 0 ; i-- ){ + b[i] = x * b[i] %m; + x = x * a[i] % m; + } + + return b; +} \ No newline at end of file diff --git a/Number Theory/modAdd.cpp b/Number Theory/modAdd.cpp new file mode 100644 index 0000000..3164a4e --- /dev/null +++ b/Number Theory/modAdd.cpp @@ -0,0 +1,4 @@ +template +T modAdd(T a,T b,T M=(T)1e9+7){ + return ((M+a%M)%M+(M+b%M)%M)%M; +} diff --git a/Number Theory/modMul.cpp b/Number Theory/modMul.cpp new file mode 100644 index 0000000..0217396 --- /dev/null +++ b/Number Theory/modMul.cpp @@ -0,0 +1,4 @@ +template +T modMul(T a,T b,T M=(int)1e9+7){ + return ((a%M)*(b%M))%M; +} \ No newline at end of file diff --git a/Number Theory/modSub.cpp b/Number Theory/modSub.cpp new file mode 100644 index 0000000..9acdad8 --- /dev/null +++ b/Number Theory/modSub.cpp @@ -0,0 +1,4 @@ +template +T modSub(T a,T b,T M=(T)1e9+7){ + return (M+((M+a%M)%M-(M+b%M)%M))%M; +} \ No newline at end of file diff --git a/String Hashing/String_Hashing.cpp b/String Hashing/String_Hashing.cpp new file mode 100644 index 0000000..284b023 --- /dev/null +++ b/String Hashing/String_Hashing.cpp @@ -0,0 +1,137 @@ +class String_hash{ + + public: + + vector p = {31,33}; + + vector prime = { 1000000007, 1000000009 }; + + int sz; + + //For Storing the hash value of the prefix string. + vector> hash; + //For storing the power of p(constant) for polynomial function. + vector> power_p; + //For storing the inverse of the power of p + vector> inv_power_p; + + String_hash(string a, int num){ + sz = num; + + hash.resize(sz); + power_p.resize(sz); + inv_power_p.resize(sz); + string s = a; + int n = (int)s.size(); + + //Pre-Calculating power. + for(int i = 0 ; i < sz ; i++ ){ + + power_p[i].resize(n); + power_p[i][0] = 1; + + for(int j = 1 ; j < n ; j++ ){ + power_p[i][j] = ( power_p[i][j-1] * p[i] ) % prime[i]; + } + } + + //Pre-Calculating inverse power. + for(int i = 0 ; i < sz ; i++ ){ + + inv_power_p[i] = invs(power_p[i] , prime[i] ); + + } + + //Calculating prefix-hash of the given string. + for(int i = 0 ; i < sz ; i++ ){ + + hash[i].resize(n); + + for(int j = 0 ; j < n ; j++ ){ + hash[i][j] = ( ( s[j] - 'a' + 1LL ) * power_p[i][j] ) % prime[i]; + hash[i][j] = ( hash[i][j] + ( j-1 >= 0 ? hash[i][j-1] : 0LL ) ) % prime[i]; + } + } + } + + vector invs( vi& a , int m ){ + int n = (int)a.size(); + + if( n == 0 ) return {}; + + vector b(n); + + int v = 1; + for(int i = 0 ; i < n ; i++ ){ + b[i] = v; + v = ((long long)v * a[i] ) % m; + } + + int x = power( v , m - 2 , m ) ; + + x = (x % m + m ) % m; + + for(int i = n - 1 ; i >= 0 ; i-- ){ + b[i] = x * b[i] %m; + x = x * a[i] % m; + } + + return b; + } + + void add_char_back(char c ){ + + + for(int i = 0 ; i < sz ; i++ ){ + + //power of p for new character adding at the back. + power_p[i].pb( power_p[i].back() * p[i] ); + hash[i].pb( (hash[i].back()) + ( c - 'a' + 1LL ) * (power_p[i].back()) % prime[i]); + } + } + + + void add_char_front(char c ){ + + for(int i = 0 ; i < sz ; i++ ){ + + //power of p for new character adding at the back. + power_p[i].pb( power_p[i].back() * p[i] ); + hash[i].pb( (hash[i].back()) * (power_p[i].back()) + ( c - 'a' + 1LL ) % prime[i]); + } + } + + vector subStringHash(int l , int r ){ //log 1e9+7 + + vector new_hash(sz); + for(int i = 0 ; i < sz ; i++ ){ + + int val1 = ( l-1 >= 0 ? hash[i][l-1] : 0LL ); + int val2 = hash[i][r]; + + new_hash[i] = modMul( modSub( val2 , val1 , prime[i]) , inv_power_p[i][l] , prime[i] ) ; + } + + return new_hash; + } + + bool compareSubString( int l1 , int r1 , int l2 , int r2 ){ + + if( l1 > l2 ){ + swap(l1,l2); + swap(r1,r2); + } + + for(int i = 0 ; i < sz ; i++ ){ + + int val1 = modSub( hash[i][r1] , ( l1 - 1 >= 0 ? hash[i][l1-1] : 0LL ), prime[i] ); + int val2 = modSub( hash[i][r2] , ( l2 - 1 >= 0 ? hash[i][l2-1] : 0LL ), prime[i] ); + + if( modMul( val1 ,power_p[i][l2-l1] , prime[i] ) != val2 ){ + return false; + } + } + return true; + } + +}; \ No newline at end of file