Skip to content

Commit ee6eff7

Browse files
author
Tanzim Hossain Romel
committed
Final
1 parent 56b4fe8 commit ee6eff7

17 files changed

+482
-0
lines changed
700 Bytes
Binary file not shown.
Binary file not shown.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
5+
6+
string LCS(string s1, string s2){
7+
int l1 = s1.length();
8+
int l2 = s2.length();
9+
10+
int c[l1+1][l2+1];
11+
12+
for(int i=0;i<=l1;i++){
13+
c[i][0] = 0;
14+
}
15+
16+
for(int i=0;i<=l2;i++){
17+
c[0][i] = 0;
18+
}
19+
20+
for(int i = 1; i<=l1; i++){
21+
for(int j=1;j<=l2;j++){
22+
if (s1[i-1] == s2[j-1]){
23+
c[i][j] = c[i-1][j-1] + 1;
24+
} else if (c[i-1][j] >= c[i][j-1]){
25+
c[i][j] = c[i-1][j];
26+
} else {
27+
c[i][j] = c[i][j-1];
28+
}
29+
}
30+
}
31+
32+
int i = l1, j = l2 ;
33+
string lcs;
34+
while(i>0 && j>0){
35+
if(c[i][j] == c[i-1][j])
36+
i--;
37+
else if (c[i][j] == c[i][j-1])
38+
j--;
39+
else{
40+
lcs += s1[i-1];
41+
i--;
42+
j--;
43+
}
44+
}
45+
reverse(begin(lcs), end(lcs));
46+
47+
return lcs;
48+
}
49+
50+
51+
52+
int main(){
53+
string s1, s2;
54+
cin>>s1;
55+
cin>>s2;
56+
cout<<LCS(s1,s2);
57+
}
69.6 KB
Binary file not shown.

Offline 8 (Disjoint Sets)/1705069.zip

1.07 KB
Binary file not shown.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
class DisjSets{
5+
private:
6+
int *parent = NULL;
7+
int *rank = NULL;
8+
int length;
9+
10+
public:
11+
DisjSets(int numOfElem){
12+
length = numOfElem;
13+
parent = new int[numOfElem];
14+
rank = new int[numOfElem];
15+
16+
for(int i=0;i<numOfElem;i++){
17+
rank[i] = 0;
18+
parent[i] = -1;
19+
}
20+
21+
}
22+
void makeSet(int v){
23+
parent[v] = v;
24+
rank[v] = 0;
25+
}
26+
27+
int findSet(int v){
28+
if(parent[v] == v)
29+
return v;
30+
else
31+
return parent[v] = findSet(parent[v]);
32+
}
33+
34+
void Union(int a, int b){
35+
36+
if (parent[a] == -1 || parent[b] == -1){
37+
cout<<"Union failed!"<<endl;
38+
return;
39+
}
40+
41+
a = findSet(a);
42+
b = findSet(b);
43+
44+
if(a!=b){
45+
if(rank[a]<rank[b])
46+
swap(a,b);
47+
parent[b] = a;
48+
if(rank[a] == rank[b])
49+
rank[a]++;
50+
}
51+
}
52+
53+
void print(int x){
54+
55+
if(parent[x] == -1){
56+
cout<<"There's no set containing "<<x<<endl;
57+
return;
58+
}
59+
60+
int rootx = findSet(x);
61+
62+
for(int i = 0; i < length; i++){
63+
if (parent[i] == -1)
64+
continue;
65+
66+
int rooti = rootx;
67+
68+
if (parent[i] != rootx)
69+
rooti = findSet(i);
70+
71+
if(rooti == rootx)
72+
cout<<i<<" ";
73+
}
74+
cout<<endl;
75+
}
76+
77+
~DisjSets(){
78+
delete[] parent;
79+
delete[] rank;
80+
}
81+
82+
};
83+
84+
int main(){
85+
DisjSets ds(100);
86+
87+
while(true){
88+
cout<<"1. Make Set "<<endl;
89+
cout<<"2. Find Set "<<endl;
90+
cout<<"3. Union "<<endl;
91+
cout<<"4. Print "<<endl;
92+
cout<<"5. Quit "<<endl;
93+
cout<<">> ";
94+
95+
int choice,n,m;
96+
cin>>choice;
97+
if(choice == 5)
98+
break;
99+
else if (choice == 1){
100+
cout<<"Enter the element: ";
101+
cin>>n;
102+
ds.makeSet(n);
103+
} else if (choice == 2){
104+
cout<<"Enter the element: ";
105+
cin>>n;
106+
cout<<"Root: "<<ds.findSet(n)<<endl;
107+
} else if (choice == 3){
108+
cout<<"Enter the elements: ";
109+
cin>>n>>m;
110+
ds.Union(n,m);
111+
} else if (choice == 4){
112+
cout<<"Enter the element: ";
113+
cin>>n;
114+
cout<<"Elements of the set containing "<<n<<" : ";
115+
ds.print(n);
116+
}
117+
}
118+
}
119+
//Nishan Test Case
120+
//1 1 1 2 1 3 1 4 1 5 3 2 3 3 4 5 3 4 1 3 4 2
121+
122+
//Kashem Sir Slide
123+
//1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8
124+
//3 2 1 3 2 3 3 2 4 3 3 5 3 3 6 3 5 7 3 6 8
205 KB
Binary file not shown.
197 KB
Binary file not shown.
266 KB
Binary file not shown.

0 commit comments

Comments
 (0)