-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestcommonsubstring.cpp
More file actions
61 lines (52 loc) · 1.17 KB
/
longestcommonsubstring.cpp
File metadata and controls
61 lines (52 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//Print the length of the longest common substring
#include <iostream>
#include <cstring>
using namespace std;
int commonsubsequence(char *x, char *y, int m, int n)
{
int ans[m + 1][n + 1];
int res = 0, row, col;
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
ans[i][j] = 0;
else if (x[i - 1] == y[j - 1])
{
ans[i][j] = ans[i - 1][j - 1] + 1;
if (res < ans[i][j])
{
res = ans[i][j];
row = i;
col = j;
}
}
else
ans[i][j] = 0;
}
}
int len = res;
if (len == 0)
{
cout << "No Common Substring";
return 0;
}
char *s = new char[len + 1];
while (ans[row][col] != 0)
{
s[--len] = x[row - 1]; // or Y[col-1]
row--;
col--;
}
cout << s << endl;
return res;
}
int main()
{
char x[] = "prashanthkammar";
char y[] = "prashanth";
int m = strlen(x);
int n = strlen(y);
cout << commonsubsequence(x, y, m, n);
}