Skip to content

Commit 8a78ee4

Browse files
committed
Add basic programming templates
0 parents  commit 8a78ee4

10 files changed

+345
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a.out

LCS.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//Longest Common Subsequence
2+
//Time complexity O(mn)
3+
//Space complexity O(mn)
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
void printLCS(char s1[], char s2[]) {
8+
int i,j;
9+
int m = strlen(s1);
10+
int n = strlen(s2);
11+
int LCS[m+1][n+1];
12+
for(i=0; i<=m; i++) {
13+
for(j=0; j<=n; j++) {
14+
if(i==0 || j==0)
15+
LCS[i][j] = 0;
16+
else if(s1[i-1] == s2[j-1])
17+
LCS[i][j] = 1 + LCS[i-1][j-1];
18+
else
19+
LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1]);
20+
}
21+
}
22+
int index = LCS[m][n];
23+
char lcs[index+1];
24+
lcs[index] = '\0';
25+
i = m; j = n;
26+
while(i > 0 && j > 0) {
27+
if(s1[i-1] == s2[j-1]) {
28+
lcs[index-1] = s1[i-1];
29+
i--; j--; index--;
30+
}
31+
else if(LCS[i-1][j] > LCS[i][j-1])
32+
i--;
33+
else
34+
j--;
35+
}
36+
cout<<lcs<<endl;
37+
}
38+
39+
int main() {
40+
char s1[30],s2[30];
41+
cin>>s1;
42+
cin>>s2;
43+
printLCS(s1,s2);
44+
return 0;
45+
}

LPS.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Longest palindromic substring in O(n^2) time and O(1) space
2+
3+
#include <iostream>
4+
#include <string.h>
5+
using namespace std;
6+
7+
int longPalSubstr(char str[]) {
8+
int n = strlen(str);
9+
int i,low,high,len=1,start=0,maxlen=1;
10+
for(i=1;i<n;i++) {
11+
//find even length substrings centred at i-1 and i
12+
low=i-1; high=i;
13+
while(low>=0 && high<n && str[low]==str[high]) {
14+
len = high-low+1;
15+
low--; high++;
16+
}
17+
if(len>maxlen) {
18+
maxlen = len;
19+
start = i - len/2;
20+
}
21+
22+
//find odd length substring centred at i
23+
low=i-1; high=i+1;
24+
while(low>=0 && high<n && str[low]==str[high]) {
25+
len = high-low+1;
26+
low--; high++;
27+
}
28+
if(len>maxlen) {
29+
maxlen = len;
30+
start = i - len/2;
31+
}
32+
}
33+
cout<<"Longest palindromic substring : ";
34+
for(i=start;i<start+maxlen;i++)
35+
cout<<str[i];
36+
return maxlen;
37+
}
38+
39+
int main() {
40+
char str[20];
41+
cin>>str;
42+
cout<<"\nLength : "<<longPalSubstr(str)<<"\n";
43+
return 0;
44+
}

bfs.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
using namespace std;
4+
5+
struct node {
6+
bool visited;
7+
ll dist;
8+
vector<ll> adj;
9+
};
10+
11+
int main() {
12+
ll n,e,i,s,u,v,d;
13+
cin>>n>>e;
14+
vector<node> graph(n+1);
15+
for(i=0;i<n;i++) {
16+
graph[i].visited = false;
17+
graph[i].dist = 0;
18+
}
19+
for(i=0;i<e;i++) {
20+
cin>>u>>v;
21+
graph[u].adj.push_back(v);
22+
graph[v].adj.push_back(u);
23+
}
24+
cin>>s;
25+
queue<ll> q;
26+
q.push(s);
27+
graph[s].dist = 0;
28+
graph[s].visited = true;
29+
while(!q.empty()) {
30+
s = q.front();
31+
q.pop();
32+
cout<<s<<" "<<graph[s].dist<<endl;
33+
for(i=0;i<graph[s].adj.size();i++) {
34+
d = graph[s].adj[i];
35+
if(!graph[d].visited) {
36+
graph[d].visited = true;
37+
graph[d].dist = graph[s].dist + 1;
38+
q.push(d);
39+
}
40+
}
41+
}
42+
return 0;
43+
}

disjointsets.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include <vector>
3+
#define ll long long
4+
using namespace std;
5+
6+
void make_set(vector<ll> &id, vector<ll> &rank, ll n) {
7+
for(ll i=1; i<=n; i++) {
8+
id[i] = i;
9+
rank[i] = 1;
10+
}
11+
}
12+
13+
ll find_set(vector<ll> &id, ll p) {
14+
ll root = p;
15+
//find the root
16+
while(root != id[root]) {
17+
root = id[root];
18+
}
19+
//perform path compression
20+
while(p != root) {
21+
ll newp = id[p];
22+
id[p] = root;
23+
p = newp;
24+
}
25+
return root;
26+
}
27+
28+
void merge_set(vector<ll> &id, vector<ll> &rank, ll x, ll y) {
29+
x = find_set(id, x);
30+
y = find_set(id, y);
31+
if(rank[x] > rank[y])
32+
id[y] = x;
33+
else
34+
id[x] = y;
35+
if(rank[x] == rank[y])
36+
rank[y] += 1;
37+
}
38+
39+
int main() {
40+
ll i,n,res = 0;
41+
cin>>n;
42+
vector<ll> forest(n+1), rank(n+1), id(n+1);
43+
for(i=1;i<=n;i++) {
44+
cin>>forest[i];
45+
}
46+
make_set(id, rank, n);
47+
for(i=1;i<=n;i++) {
48+
merge_set(id, rank, i, forest[i]);
49+
}
50+
for(i=1;i<=n;i++) {
51+
if(id[i] == i)
52+
res++;
53+
}
54+
cout<<res;
55+
return 0;
56+
}
57+

editDistance.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <string.h>
4+
using namespace std;
5+
6+
int editDistance(char s1[],char s2[]) {
7+
int m=strlen(s1);
8+
int n=strlen(s2);
9+
int dp[m+1][n+1];
10+
for(int i=0;i<=m;i++) {
11+
for(int j=0;j<=n;j++) {
12+
if(i==0)
13+
dp[i][j]=j;
14+
else if(j==0)
15+
dp[i][j]=i;
16+
else if(s1[i-1]==s2[j-1])
17+
dp[i][j]=dp[i-1][j-1];
18+
else
19+
dp[i][j]=1+min(dp[i][j-1],min(dp[i-1][j],dp[i-1][j-1]));
20+
}
21+
}
22+
return dp[m][n];
23+
}
24+
25+
int main() {
26+
char s1[30],s2[30];
27+
cout<<"Hi";
28+
cin>>s1;
29+
cin>>s2;
30+
cout<<"\nEdit distance : "<<editDistance(s1,s2)<<"\n";
31+
return 0;
32+
}

extendedEuclid.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
using namespace std;
4+
5+
ll xGCD(ll a, ll b, ll &x, ll &y) {
6+
if(b == 0) {
7+
x = 1;
8+
y = 0;
9+
return a;
10+
}
11+
12+
ll x1, y1, gcd = xGCD(b, a % b, x1, y1);
13+
x = y1;
14+
y = x1 - (a / b) * y1;
15+
return gcd;
16+
}
17+
18+
int main() {
19+
ll a,b,c,x,y;
20+
cin>>a>>b>>c;
21+
ll gcd = xGCD(a,b,x,y);
22+
if(c%gcd == 0) {
23+
ll factor = c/gcd;
24+
cout<<"x = "<<x*factor<<"\ny = "<<y*factor<<endl;
25+
}
26+
else {
27+
cout<<"Solution does not exist\n";
28+
}
29+
return 0;
30+
}

segmentedSieve.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//Program to generate all primes in the range[m,n]
2+
//seg_sieve[i] indicates whether i+m is prime or not
3+
#include <bits/stdc++.h>
4+
#define ll long long
5+
using namespace std;
6+
7+
int main() {
8+
ll m,n;
9+
cin>>m>>n;
10+
ll rt = ceil(sqrt(n)) + 1;
11+
vector<bool> primes(rt + 1), seg_sieve(n - m + 1);
12+
for (ll i = 2; i <= rt; ++i) {
13+
if (primes[i]) continue;
14+
for (ll j = i + i; j <= rt; j += i)
15+
primes[j] = true;
16+
for (ll j = max(2LL, (m + i - 1) / i) * i; j <= n; j += i)
17+
seg_sieve[j - m] = true;
18+
}
19+
return 0;
20+
}

subsequenceDivisibility.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//given a number having N digits, find number of subsequences of the number divisible by D
2+
//Time Complexity : O(nd)
3+
//Space Complexity : O(1)
4+
#include <bits/stdc++.h>
5+
#define ll long long int
6+
#define d 8
7+
using namespace std;
8+
9+
int main() {
10+
ll n,i,r;
11+
ll cnst = 1e9 + 7;
12+
cin>>n;
13+
int dp[2][d] = {0};
14+
char s[n+5];
15+
cin>>s;
16+
dp[0][0] = 1;
17+
for(i=0;i<n;i++) {
18+
for(r=0;r<d;r++) {
19+
dp[1][(10*r + int(s[i]-'0'))%d] = (dp[1][(10*r + int(s[i]-'0'))%d] + dp[0][r])%cnst;
20+
dp[1][r] = (dp[1][r] + dp[0][r])%cnst;
21+
}
22+
for(r=0;r<d;r++) {
23+
dp[0][r] = dp[1][r];
24+
dp[1][r] = 0;
25+
}
26+
}
27+
cout<<dp[0][0] - 1;
28+
return 0;
29+
}

unionOfIntervals.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
struct interval {
7+
int s;
8+
int e;
9+
};
10+
11+
bool comp(interval a, interval b) {
12+
return a.s>b.s;
13+
}
14+
15+
void mergeIntervals(vector<interval> &a, int n) {
16+
sort(a.begin(),a.end(),comp);
17+
int index = 0;
18+
19+
for(int i=0;i<n;i++) {
20+
if(index != 0 && a[index-1].s <= a[i].e) {
21+
while(index != 0 && a[index-1].s <= a[i].e) {
22+
a[index-1].s = min(a[index-1].s, a[i].s);
23+
a[index-1].e = max(a[index-1].e, a[i].e);
24+
index--;
25+
}
26+
}
27+
else {
28+
a[index] = a[i];
29+
}
30+
index++;
31+
}
32+
for(int i=0;i<index;i++)
33+
cout<<a[i].s<<" "<<a[i].e<<"\n";
34+
}
35+
36+
int main() {
37+
int n;
38+
cin>>n;
39+
vector<interval> a(n);
40+
for(int i=0;i<n;i++)
41+
cin>>a[i].s>>a[i].e;
42+
mergeIntervals(a,n);
43+
return 0;
44+
}

0 commit comments

Comments
 (0)