This repository has been archived by the owner on Oct 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from Anshika-G/master
Added longest common subsequence
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
data_structures/linked_lists/linked_list_implementation.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
//Declare Node | ||
struct Node{ | ||
int num; | ||
Node *next; | ||
}; | ||
|
||
//Declare starting (Head) node | ||
struct Node *head=NULL; | ||
|
||
//Insert node at start | ||
void insertNode(int n){ | ||
struct Node *newNode=new Node; | ||
newNode->num=n; | ||
newNode->next=head; | ||
head=newNode; | ||
} | ||
|
||
//Traverse/ display all nodes (print items) | ||
void display(){ | ||
if(head==NULL){ | ||
cout<<"List is empty!"<<endl; | ||
return; | ||
} | ||
struct Node *temp=head; | ||
while(temp!=NULL){ | ||
cout<<temp->num<<" "; | ||
temp=temp->next; | ||
} | ||
cout<<endl; | ||
} | ||
|
||
//delete node from start | ||
void deleteItem(){ | ||
if(head==NULL){ | ||
cout<<"List is empty!"<<endl; | ||
return; | ||
} | ||
cout<<head->num<<" is removed."<<endl; | ||
head=head->next; | ||
} | ||
int main(){ | ||
|
||
display(); | ||
insertNode(10); | ||
insertNode(20); | ||
insertNode(30); | ||
insertNode(40); | ||
insertNode(50); | ||
display(); | ||
deleteItem(); deleteItem(); deleteItem(); deleteItem(); deleteItem(); | ||
deleteItem(); | ||
display(); | ||
return 0; | ||
} |
40 changes: 40 additions & 0 deletions
40
dynamic_programing/longest_common_subsequence/c++/longest_common_subsequence.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include<iostream> | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
int lpsH(string s, int start, int end, int **output){ | ||
if(start > end){ | ||
return 0; | ||
} | ||
|
||
if(s.length() == 1 || start == end){ | ||
return 1; | ||
} | ||
|
||
if(output[start][end] > -1){ | ||
return output[start][end]; | ||
} | ||
if(s[start] == s[end]){ | ||
output[start][end] = 2 + lpsH(s, start+1, end-1, output); | ||
return output[start][end]; | ||
} | ||
output[start][end] = max(lpsH(s, start+1, end, output), lpsH(s, start, end-1, output)); | ||
return output[start][end]; | ||
} | ||
int lcs(string s1, string s2){ | ||
int n1 = s1.length(); | ||
int **output = new int*[n1+1]; | ||
|
||
for(int i = 0; i <= s1.length(); i++){ | ||
output[i] = new int[s2.length()+1]; | ||
for(int j = 0; j <= s2.length(); j++){ | ||
output[i][j] = -1; | ||
} | ||
} | ||
return lpsH(s1, 0, s1.length()-1, output); | ||
} | ||
int main(){ | ||
string s1, s2; | ||
cin >> s1 >> s2; | ||
cout << lcs(s1, s2); | ||
} |