forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOJ1458(AC).cpp
120 lines (105 loc) · 1.29 KB
/
POJ1458(AC).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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *a;
char *b;
char *tmp;
int min(int a, int b)
{
if(a > b)
{
return b;
}
else
{
return a;
}
}
int max(int a, int b)
{
if(a > b)
{
return a;
}
else
{
return b;
}
}
int same(int i, int j)
{
return a[i] == b[j];
}
int main()
{
int i;
int j;
int lena;
int lenb;
int cur;
int f[2][10000];
a = (char *)malloc(10000 * sizeof(char));
b = (char *)malloc(10000 * sizeof(char));
tmp = (char *)malloc(10000 * sizeof(char));
while(1)
{
if(scanf("%s%s", a, b) != 2)
{
break;
}
if(strlen(a) < strlen(b))
{
strcpy(tmp, a);
strcpy(a, b);
strcpy(b, tmp);
}
lena = strlen(a);
lenb = strlen(b);
for(i = 0; i < 10000; i++)
{
f[0][i] = f[1][i] = 0;
}
if(lenb == 1)
{
for(i = 0; i < lena; i++)
{
if(a[i] == b[0])
{
break;
}
}
if(i == lena)
{
printf("0\n");
}
else
{
printf("1\n");
}
}
else
{
cur = 0;
for(i = 0; i < lena; i++)
{
for(j = 0; j < lenb; j++)
{
if(a[i] == b[j])
{
f[cur][j + 1] = f[!cur][j] + 1;
}
else
{
f[cur][j + 1] = max(f[cur][j], f[!cur][j + 1]);
}
}
cur = !cur;
}
printf("%d\n", f[!cur][lenb]);
}
}
free(a);
free(b);
free(tmp);
return 0;
}