HDU 1501 Zipper
题意:给3个字符串,问第三个串能不能由前两个构成,在不改变相互顺序的情况下!
思路:记忆化搜索,开个hash数组保存,否则超时~
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }