-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10405_uva.cpp
More file actions
56 lines (48 loc) · 1.04 KB
/
10405_uva.cpp
File metadata and controls
56 lines (48 loc) · 1.04 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
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
int lcs_search(char * s1, char *s2){
int len_s1=strlen(s1);
int len_s2=strlen(s2);
if((len_s1==0) || (len_s2==0)) return 0;
int ** matrix= new int*[len_s2];
for (int i=0;i<len_s2;++i){
matrix[i]=new int[len_s1];
}
for (int i=0;i<len_s2;i++){
for(int j=0;j<len_s1;j++){
int left_val=0;
int top_val=0;
int diag_up=0;
if(j>=1)left_val=matrix[i][j-1];
if(i>=1)top_val=matrix[i-1][j];
if(i>=1 && j>=1) diag_up=matrix[i-1][j-1];
if(s1[j]==s2[i]){
matrix[i][j]=diag_up+1;
}
else{
matrix[i][j]=max(left_val,top_val);
}
}
}
int ret_val=matrix[len_s2-1][len_s1-1];
for (int i=0;i<len_s2;i++){
delete [] matrix[i];
}
delete [] matrix;
return ret_val;
}
int main(){
char s1[1001];
char s2[1001];
while(gets(s1)){/// important to use gets here so that white spaces aren't consumed
gets(s2);
int res=lcs_search(s1,s2);
printf("%d\n",res);
}
return 0;
}