A Za, A Za, Fighting...

坚信:勤能补拙

PKU 2192 Zipper

问题:
http://acm.pku.edu.cn/JudgeOnline/problem?id=2192

思路:
原本以为是类似于PKU 1936的简单题,结果Sample测试不过,发现对于像cat, tree这样包含相同字母(这里是t)的例子需要回溯,于是DFS,这样结果虽然正确了,但是却TLE...
准确的做法是动态规划,艾,今天三题没有一个是自己想出来的...悲剧...
详细的状态转化方程见代码注释

代码:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define MAX_LEN 201
 5 char first[MAX_LEN+1], second[MAX_LEN+1];
 6 char final[MAX_LEN*2];
 7 int flen, slen, tlen;
 8 int table[MAX_LEN][MAX_LEN];
 9 
10 /* 
11  * f[i][j] represent whether final[1..i+j] could be formed from first[1..i] and second[1..j]
12  * f[i][j] is true if:
13  *         a. final[i+j]==first[i] && f[i-1][j] is true, or
14  *         b. final[i+j]==second[j] && f[i][j-1] is true
15  */
16 int 
17 dp()
18 {
19     int i, j, mark;
20     mark = 1;
21     for(i=1; i<=flen; i++) {
22         if(first[i]==final[i] && mark)
23             table[i][0= 1;
24         else {
25             table[i][0= 0;
26             mark = 0;
27         }
28     }
29     mark = 1;
30     for(j=1; j<=slen; j++) {
31         if(second[j]==final[j] && mark)
32             table[0][j] = 1;
33         else {
34             table[0][j] = 0;
35             mark = 0;
36         }
37     }
38     for(i=1; i<=flen; i++) {
39         for(j=1; j<=slen; j++) {
40             if((final[i+j]==first[i]&&table[i-1][j]) || (final[i+j]==second[j]&&table[i][j-1]))
41                 table[i][j] = 1;
42             else
43                 table[i][j] = 0;
44         }
45     }
46     return table[flen][slen];
47 }
48 
49 int
50 main(int argc, char **argv)
51 {
52     int tests, cnt=0;
53     scanf("%d"&tests);
54     while(tests--) {
55         scanf("%s %s %s", first+1, second+1, final+1);
56         flen = strlen(first+1);
57         slen = strlen(second+1);
58         tlen = strlen(final+1);
59         printf("Data set %d: %s\n"++cnt, dp()?"yes":"no");
60     }
61 }

posted on 2010-08-13 22:33 simplyzhao 阅读(179) 评论(0)  编辑 收藏 引用 所属分类: C_动态规划


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2011年8月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜