-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomString.cpp
More file actions
51 lines (39 loc) · 1006 Bytes
/
comString.cpp
File metadata and controls
51 lines (39 loc) · 1006 Bytes
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
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main () {
string first;
string second;
cin >> first;
cin >> second;
int n = first.size();
int m = second.size();
int matrix[n][m];
for (int k = 0; k < n; ++k) {
for (int l = 0; l < m; ++l) {
if (first[k] == second[l]) {
matrix[k][l] = 1;
} else {
matrix[k][l] = 0;
}
};
};
int currVal = 0;
int currI;
int currJ;
for (int i = 1; i < n; ++i) {
for (int j = 1; j < m; ++j) {
if (matrix[i][j] != 0) {
matrix[i][j] = matrix[i][j] + matrix[i-1][j-1];
if (matrix[i][j] > currVal) {
currVal = matrix[i][j];
currI = i;
currJ = j;
};
}
};
};
cout << currVal << endl;
cout << first.substr(currI-currVal+1, currVal) << endl;
}