Skip to content

new codes added #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions CPP/Detect_Loop_Using_Floyd’s Cycle-Finding Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include<bits/stdc++.h>
using namespace std;

class Node {
public:
int data;
Node* next;
};

//Pushing a Node to Linked List
void push(Node** head_ref, int data) {
Node* node = new Node();
node->data = data;
node->next = (*head_ref);
(*head_ref) = node;
}

//Detection of Loop using Floyd’s Cycle-Finding Algorithm
bool detectLoop(Node* head){
Node* s=head;Node* f=head;
while(s!=NULL && f!=NULL && f->next!=NULL) {
s=s->next;
f=f->next->next;
if(f==s) return true;
}
return false;
}

int main() {

Node* head = NULL;
// push the desired nodes
push(&head, 2);
push(&head, 20);
push(&head, 40);
push(&head, 50);
push(&head, 75);

// Sample Loop
head->next->next->next = head;
if (detectLoop(head))
cout << "Loop is Found";
else
cout << "No Loop is Found";
return 0;
}
28 changes: 28 additions & 0 deletions CPP/largest_repeating_subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;

int longRepSubseq(string a){
int n = a.length();

//initialize DP table
int dp[n+1][n+1];
memset(dp,0,sizeof(dp));

for (int i=1; i<=n; i++){
for (int j=1; j<=n; j++){
// Characters match but indexes are not same
if (a[i-1] == a[j-1] && i != j)
dp[i][j] = 1 + dp[i-1][j-1];
//characters do not match
else
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
return dp[n][n];
}

int main(){
string a = "aabbcdee";
cout << "Length of Largest Repeating Subsequence is "<< longRepSubseq(a);
return 0;
}
35 changes: 35 additions & 0 deletions CPP/longest_common_subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<bits/stdc++.h>
using namespace std;

//find length of longest common subsequence
int lcs(string a,string b,int m,int n) {

int dp[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
//dp[i][j] contains lcs of string a(0..i) and b(0..j)
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (a[i - 1] == b[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[m][n];
}


int main() {
//desired strings are taken
string a = "longest";
string b = "longsequence";

int m = a.size();
int n = b.size();

//Print the length of longest common subsequence
cout << "The length of Longest Common Subsequence is " << lcs(a,b,m,n);

return 0;
}
1 change: 1 addition & 0 deletions Contributor.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
* [Shreya Punetha](https://github.com/shreyapuntha)
* [Sahil Jamwal](https://github.com/sahiljamwal)
* [Ganapathy Rama Subramanian](https://github.com/Gunsubramanian)
* [Shlok Kyal](https://github.com/skkyal)