diff --git a/README.md b/README.md index c346d4f..e2fafde 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,6 @@ Happy Open Sourcing! - [Caesar's Cipher](algorithms/Caesar's_cipher) - [Array Rotation By 1](algorithms/array_rotation_by_1) - [Magic Number](algorithms/magic_no/) +- [KMP Algorithm](algorithms/Knuth-Morris-Pratt/) +- [Matrix Exponentiation](algorithms/Matrix-Expo/) +- [Disjoint Set Union](algorithms/disjoint_set_union/) diff --git a/algorithms/Knuth-Morris-Pratt/README.md b/algorithms/Knuth-Morris-Pratt/README.md new file mode 100644 index 0000000..8a4abcd --- /dev/null +++ b/algorithms/Knuth-Morris-Pratt/README.md @@ -0,0 +1,29 @@ +# Knuth-Morris-Pratt + +An Algorithm For searching a string pattern in a different string + +### Input Format + +Input: First String + Second String + +### Output Format + +Output: (Number Of Occurences) + +### Sample Input + +``` +abcde +abcdeaaaaabcdehlkj +``` + +### Sample Output + +``` +2 +``` + +### Implemented in: + +- [C++](kmpalgo.cpp) \ No newline at end of file diff --git a/algorithms/Knuth-Morris-Pratt/kmpalgo.cpp b/algorithms/Knuth-Morris-Pratt/kmpalgo.cpp new file mode 100644 index 0000000..ed7971d --- /dev/null +++ b/algorithms/Knuth-Morris-Pratt/kmpalgo.cpp @@ -0,0 +1,43 @@ +//KMP Algo +#include +#include +using namespace std; + +int main(){ + string s,str; + cin>>s>>str; + long sz=s.length(),cnt=0; + long a[sz]; + a[0]=0; + long l=0,r=1; + while(r +#include + +using namespace std; + +void mulMatrix(int a[][20],int b[][20],int n){ + int c[20][20]; + for(int i=0;i0){ + if(p&1) + mulMatrix(res,a,n); + mulMatrix(a,a,n); + p>>=1; + } + for(int i=0;i>n>>m; + for(int i=0;i>a[i][j]; + } + MatExpo(a,n,m); + for(int i=0;i +#include +using namespace std; + +struct node{ + long data; + long rank; + node* parent; +}; + +void makeSet(map &mp,long data){ + node* temp=new node; + temp->data=data; + temp->rank=0; + temp->parent=temp; + mp[data]=temp; +} + +node* findRoot(map &mp,long data){ + auto it=mp.find(data); + if(it==mp.end()) + return NULL; + node* temp=it->second; + if(temp->parent==temp) + return temp; + temp->parent=findRoot(mp,temp->parent->data); + return temp->parent; +} + +void Union(map &mp,long data1,long data2){ + auto it1=mp.find(data1); + auto it2=mp.find(data2); + if(it1==mp.end() || it2==mp.end()) + return; + node* a=findRoot(mp,data1); + node* b=findRoot(mp,data2); + if(a==b) + return; + if(a->rank==b->rank){ + b->parent=a; + a->rank++; + } + else if(a->rank>b->rank) + b->parent=a; + else + a->parent=b; +} + +bool Find(map &mp,long data1,long data2){ + auto it1=mp.find(data1); + auto it2=mp.find(data2); + if(it1==mp.end() || it2==mp.end()) + return 0; + node* a=findRoot(mp,data1); + node* b=findRoot(mp,data2); + if(a==b) + return 1; + return 0; +} + +int main(){ + map mp; + for(long i=1;i<=10;++i){ + makeSet(mp,i); + } + Union(mp,1,2); + Union(mp,2,3); + Union(mp,1,3); + Union(mp,8,5); + Union(mp,5,6); + node* x=findRoot(mp,3); + node* y=findRoot(mp,5); + cout<data<<" "<data; + Union(mp,3,6); + x=findRoot(mp,3); + cout<<" "<data; +} \ No newline at end of file