HDU-1501 Zipper 简单DP

题目链接



状态dp[i][j]表示字符串1的前i个字符与字符串2的前j个字符是否能构成字符串3的前i+j个字符

#include <stdio.h>
#include <string.h>
#include<functional>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 215;
const int inf = 1<<30;
typedef __int64 LL;
int len1,len2;
bool dp[maxn][maxn];
char str1[maxn],str2[maxn],str[maxn<<1];
void GetDp()
{
	memset( dp,0,sizeof(dp) );
	dp[0][0] = 1;
	for( int i = 0; i <= len1; i ++ ){
		for( int j = 0; j <= len2; j ++ ){
			if( i == j && i == 0 )	continue;
			if( i != 0 && str1[i-1] == str[i+j-1] && dp[i-1][j] )
				dp[i][j] = 1;
			else if(  j != 0 && str2[j-1] == str[i+j-1] && dp[i][j-1] )
				dp[i][j] = 1;
		}
	}
}
int main()
{
	#ifndef ONLINE_JUDGE  
	freopen("data.txt","r",stdin);  
	#endif  
	int cas;
	scanf("%d",&cas);
	for( int i = 1; i <= cas; i ++ )
	{
		scanf("%s%s%s",str1,str2,str);
		len1 = strlen( str1 );
		len2 = strlen( str2 );
		printf("Data set %d: ",i);
		GetDp();
		if( dp[len1][len2] )
			puts("yes");
		else
			puts("no");
	}
	return 0;
}


DFS

#include <stdio.h>
#include <string.h>
#include<functional>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 215;
const int inf = 1<<30;
typedef __int64 LL;
int len,len1,len2;
char str1[maxn],str2[maxn],str[maxn<<1];
int flag;
bool Hash[maxn][maxn];
bool DFS( int k,int i,int j )
{
	if( str[k] == '\0' ) return 1;
	if( Hash[i][j] ) return 0;
	Hash[i][j] = 1;
	if( str1[i] == str[k] && DFS( k+1,i+1,j) )return 1;
	if( str2[j] == str[k] && DFS( k+1,i,j+1) ) return 1;
	return 0;
}
int main()
{
	#ifndef ONLINE_JUDGE  
	freopen("data.txt","r",stdin);  
	#endif  
	int cas,c = 1;
	scanf("%d",&cas);
	while( cas -- )
	{
		flag = 0;
		scanf("%s%s%s",str1,str2,str);
		memset( Hash,0,sizeof(Hash) );
		printf("Data set %d: ",c++);
		if( DFS(0,0,0) )
			puts("yes");
		else
			puts("no");
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值