Skip to content

Commit 5ee17fe

Browse files
imkakaMadhavBahl
authored andcommitted
Day11 (#123)
* Add @imkaka as a contributor * day4 * day11
1 parent 8cefa4c commit 5ee17fe

File tree

2 files changed

+145
-4
lines changed

2 files changed

+145
-4
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @author : imkaka
3+
* @date : 04/01/2019
4+
*/
5+
6+
#include<iostream>
7+
#include<string>
8+
9+
using namespace std;
10+
11+
void longestCommonSubstrig(string, string);
12+
13+
int main(){
14+
15+
string str1 = "bisect", str2 = "secret";
16+
longestCommonSubstrig(str1, str2); // sec
17+
longestCommonSubstrig("abcxyzey", "cxyazeal"); //cxy
18+
return 0;
19+
}
20+
21+
void longestCommonSubstrig(string str1, string str2){
22+
23+
int m = str1.size();
24+
int n = str2.size();
25+
26+
int LCW[m+1][n+1];
27+
28+
//Track Len and location.
29+
int maxLCW = 0;
30+
int loc = m+1;
31+
32+
for(int i = 0; i <= m; ++i){
33+
LCW[i][n] = 0;
34+
}
35+
36+
for(int i = 0; i <= n; ++i){
37+
LCW[m][i] = 0;
38+
}
39+
40+
// Calculating the max length of common subword
41+
for(int c = n-1; c >= 0; --c){
42+
for(int r = m-1; r >= 0; --r){
43+
if(str1[r] == str2[c]){
44+
LCW[r][c] = 1 + LCW[r+1][c+1];
45+
}
46+
47+
else{
48+
LCW[r][c] = 0;
49+
}
50+
51+
if(LCW[r][c] > maxLCW){
52+
53+
maxLCW = LCW[r][c];
54+
loc = r;
55+
}
56+
}
57+
}
58+
59+
// Finding the Common SubWord
60+
string re = "";
61+
for(int r = loc; r < loc + maxLCW; ++r){
62+
re += char(str1[r]);
63+
}
64+
65+
cout << re << endl;
66+
67+
}

day11/README.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function longestSubstring (str1, str2) {
8080
break;
8181
}
8282
}
83-
83+
8484
// Return the reverse of maxSubStr
8585
return maxSubStr.split('').reverse().join('');
8686
}
@@ -114,7 +114,7 @@ int main() {
114114
int lcs[n + 1][m + 1];
115115

116116
int ma = 0;
117-
int mi = 0;
117+
int mi = 0;
118118
int mj = 0;
119119
for(int i =0; i <=n; i++){
120120
for(int j = 0; j <=m;j++){
@@ -126,7 +126,7 @@ int main() {
126126
mj = j;
127127
ma = lcs[i][j];
128128
}
129-
129+
130130
}
131131
else{
132132
lcs[i][j]= 0;
@@ -144,10 +144,84 @@ int main() {
144144
}
145145
}
146146
reverse(ans.begin(), ans.end());
147-
cout << "len : " << ma << " is " << ans;
147+
cout << "len : " << ma << " is " << ans;
148148
}
149149
```
150150

151+
### [Solution by @imkaka](./C++/longetCommonSubstring.cpp)
152+
153+
```cpp
154+
155+
/*
156+
* @author : imkaka
157+
* @date : 04/01/2019
158+
*/
159+
160+
#include<iostream>
161+
#include<string>
162+
163+
using namespace std;
164+
165+
void longestCommonSubstrig(string, string);
166+
167+
int main(){
168+
169+
string str1 = "bisect", str2 = "secret";
170+
longestCommonSubstrig(str1, str2); // sec
171+
longestCommonSubstrig("abcxyzey", "cxyazeal"); //cxy
172+
return 0;
173+
}
174+
175+
void longestCommonSubstrig(string str1, string str2){
176+
177+
int m = str1.size();
178+
int n = str2.size();
179+
180+
int LCW[m+1][n+1];
181+
182+
//Track Len and location.
183+
int maxLCW = 0;
184+
int loc = m+1;
185+
186+
for(int i = 0; i <= m; ++i){
187+
LCW[i][n] = 0;
188+
}
189+
190+
for(int i = 0; i <= n; ++i){
191+
LCW[m][i] = 0;
192+
}
193+
194+
// Calculating the max length of common subword
195+
for(int c = n-1; c >= 0; --c){
196+
for(int r = m-1; r >= 0; --r){
197+
if(str1[r] == str2[c]){
198+
LCW[r][c] = 1 + LCW[r+1][c+1];
199+
}
200+
201+
else{
202+
LCW[r][c] = 0;
203+
}
204+
205+
if(LCW[r][c] > maxLCW){
206+
207+
maxLCW = LCW[r][c];
208+
loc = r;
209+
}
210+
}
211+
}
212+
213+
// Finding the Common SubWord
214+
string re = "";
215+
for(int r = loc; r < loc + maxLCW; ++r){
216+
re += char(str1[r]);
217+
}
218+
219+
cout << re << endl;
220+
221+
}
222+
223+
```
224+
151225
## Java Implementation
152226
153227
### [Solution](./Java/longestCommonSubstring.java)

0 commit comments

Comments
 (0)