-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10405.cpp
51 lines (46 loc) · 1.19 KB
/
P10405.cpp
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 <stdio.h>
#define MAX(a,b) ((a) < (b) ? (b) : (a))
int longestSubSequence(char const * const s1, int lenS1, char const * const s2, int lenS2) {
if(lenS1 == 0 || lenS2 == 0)
return 0;
int *a = new int[2*(lenS1+1)]; // +1 for empty first character.
int prevColumn = 0;
for(int i = 0; i <= lenS1; ++i)
a[i] = 0;
for(int s2I = 0; s2I < lenS2; ++s2I) {
int startI = (1-prevColumn)*(lenS1+1);
int startPrevI = prevColumn*(lenS1+1);
a[startI] = 0; // empty first character.
for(int s1I = 0; s1I < lenS1; ++s1I) {
if(s1[s1I] == s2[s2I]) {
a[startI+s1I+1] = a[startPrevI+s1I] + 1;
}
else {
int idxA = startI+s1I;
int idxB = startPrevI+s1I+1;
a[startI+s1I+1] = MAX(a[idxA], a[idxB]);
}
//std::cerr << a[startI+s1I+1] << " ";
}
prevColumn = 1-prevColumn;
//std::cerr << std::endl;
}
int ret = a[prevColumn*(lenS1+1)+lenS1];
delete[] a;
return ret;
}
int len(char *s) {
int ret = 0;
while(isprint(s[ret]))
++ret;
return ret;
}
int main() {
char s1[1009], s2[1009];
while(gets(s1)) {
gets(s2);
std::cout << longestSubSequence(s1, len(s1), s2, len(s2)) << std::endl;
}
return 0;
}