UVa 10192 - Vacation

又是lcs,所幸 7min AC了。

 1 # include <stdio.h>
2 # include <string.h>
3
4 int len;
5 int x[2];
6 char s1[105];
7 char s2[105];
8 int ans[105*105];
9
10 int lcs(int *x);
11
12 int main()
13 {
14 int cnt;
15
16 cnt = 0;
17 while (NULL != gets(s1))
18 {
19 if (s1[0] == '#') break;
20 gets(s2);
21 x[0] = len = strlen(s1);
22 x[1] = strlen(s2);
23 memset(ans, 0xff, sizeof(ans));
24 printf("Case #%d: you can visit at most %d cities.\n", ++cnt, lcs(x));
25 }
26
27 return 0;
28 }
29
30 int lcs(int *x)
31 {
32 int index, ret, rem;
33
34 if (!x[0] || !x[1]) return 0;
35 index = x[0]-1 + (x[1]-1)*len;
36 if (ans[index] >= 0) return ans[index];
37 if (s1[x[0]-1] == s2[x[1]-1])
38 {
39 --x[0]; --x[1];
40 ret = lcs(x) + 1;
41 ++x[0]; ++x[1];
42 }
43 else
44 {
45 ret = 0;
46 --x[0]; if ((rem = lcs(x)) > ret) ret = rem; ++x[0];
47 --x[1]; if ((rem = lcs(x)) > ret) ret = rem; ++x[1];
48 }
49 ans[index] = ret;
50
51 return ret;
52 }



posted on 2012-04-02 10:22  getgoing  阅读(380)  评论(0编辑  收藏  举报

导航