poj 2192-动态规划-DP


Zipper
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13940 Accepted: 4871

Description

Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order. 

For example, consider forming "tcraete" from "cat" and "tree": 

String A: cat 
String B: tree 
String C: tcraete 

As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree": 

String A: cat 
String B: tree 
String C: catrtee 

Finally, notice that it is impossible to form "cttaree" from "cat" and "tree". 

Input

The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line. 

For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive. 

Output

For each data set, print: 

Data set n: yes 

if the third string can be formed from the first two, or 

Data set n: no 

if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example. 

Sample Input

3
cat tree tcraete
cat tree catrtee
cat tree cttaree

Sample Output

Data set 1: yes
Data set 2: yes
Data set 3: no

dp[i][j]为str1中前i个字符和str2中前j个字符能否和str3中前i+j个匹配,如果能为1,否则为0;


动态方程:a.dp[i-1][j]==1&&str1[i-1]==str3[i+j-1]//如果str1前i-1个加上str2前j个是和str3前i+j-1个是匹配的,那么考虑str1第i-1个字符和str3中第i+j-1个字符。

#include <stdio.h>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring> 
#include <cmath>
#define setbit(x,y) x|=(1<<(y)) //将X的第Y位置1
#define clrbit(x,y) x&=~(1<<(y)) //将X的第Y位清0
#define sf scanf
#define pf printf
#define INF 1 << 29
#define eps 1e-6
const double PI = acos(-1.0);               
#define lint __int64
#define LL long long 
#define MAX 1e9 + 7
#define maxn 40005
//101^110=011 异或
#define ULLint unsigned long long //2^64-1>1.8*10^19 
#define clr(x) memset(x, 0, sizeof(x))
#define Clr(x) memset(x, -1, sizeof(x))

using namespace std;

#define SIZE 205

int dp[ SIZE ][ SIZE ];
string str1, str2, str3;

void Input(){
	cin >> str1 >> str2 >> str3;
}

bool Solution(){

	memset( dp, 0, sizeof( dp ) );

	dp[ 0 ][ 0 ] = 1;

	for ( int i = 0; i <= str1.size(); ++ i )
		for ( int j = 0; j <= str2.size(); ++ j ){
			if ( i > 0 && dp[ i - 1 ][ j ] && str1[ i - 1 ] == str3[ i + j - 1 ] )
				dp[ i ][ j ] = 1;
			if ( j > 0 && dp[ i ][ j - 1 ] && str2[ j - 1 ] == str3[ i + j - 1 ] )
				dp[ i ][ j ] = 1;
		}

	if ( dp[ str1.size() ][ str2.size() ] )
		return true;
	return false;

}

void Output( int cas ){

	printf( "Data set %d: ", cas );

	if ( Solution() )
		printf( "yes\n" );
	else
		printf( "no\n" );
}

int main(){

	int T;
	int cas = 1;

	scanf( "%d", &T );
	while ( T -- ){
		Input();
		Output( cas ++ );
	}

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值