• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

HDU 1501 Zipper

题意:给3个字符串,问第三个串能不能由前两个构成,在不改变相互顺序的情况下!

思路:记忆化搜索,开个hash数组保存,否则超时~

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501

 

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <string>
 5 #include <algorithm>
 6 #include <iostream>
 7 using namespace std ;
 8 const int N=210;
 9 
10 char str1[N],str2[N],str[N*2];
11 bool hash[N][N];
12 
13 int DP(int a,int b,int c){
14     if(str[c]=='\0') return 1;
15     if(hash[a][b]) return 0;
16     hash[a][b]=1;
17     if(str1[a]==str[c]&&DP(a+1,b,c+1)) return 1;
18     if(str2[b]==str[c]&&DP(a,b+1,c+1)) return 1;
19     return 0;
20 }
21 
22 int main(){
23  
24 //    freopen("data.in","r",stdin);
25 //    freopen("data.out","w",stdout); 
26     
27     int n,i;
28     int iCase=1;
29     while(scanf("%d",&n)!=EOF){
30         scanf("%s %s %s",str1,str2,str);
31         printf("Data set %d: ",iCase++);
32         memset(hash,0,sizeof(hash));
33         if(DP(0,0,0)) puts("yes");
34         else puts("no"); 
35     }
36     return 0;
37 }

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <string>
 5 #include <algorithm>
 6 #include <iostream>
 7 using namespace std ;
 8 const int N=210;
 9 
10 char str1[N],str2[N],str[N*2];
11 bool hash[N][N];
12 
13 int DP(int a,int b,int c){
14     if(str[c]=='\0') return 1;
15     if(hash[a][b]) return 0;
16     hash[a][b]=1;
17     if(str1[a]==str[c]&&DP(a+1,b,c+1)) return 1;
18     if(str2[b]==str[c]&&DP(a,b+1,c+1)) return 1;
19     return 0;
20 }
21 
22 int main(){
23  
24     freopen("data.in","r",stdin);
25     freopen("data.out","w",stdout); 
26     
27     int n,i;
28     int iCase=1;
29     while(scanf("%d",&n)!=EOF){
30         scanf("%s %s %s",str1,str2,str);
31         printf("Data set %d: ",iCase++);
32         memset(hash,0,sizeof(hash));
33         if(DP(0,0,0)) puts("yes");
34         else puts("no"); 
35     }
36     return 0;
37 }
posted @ 2012-04-26 14:26  Hug_Sea  阅读(106)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3