您的位置:首页 > 其它

hdu 1501 Zipper dp

2016-08-03 19:26 363 查看
[align=left]Problem Description[/align]
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".

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

3
cat tree tcraete
cat tree catrtee
cat tree cttaree

 

[align=left]Sample Output[/align]

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


最优子结构分析:如上例,如果A、B可以组成C,那么,C最后一个字母e,必定是 A 或 C 的最后一个字母组成。
C去除除最后一位,就变成是否可以求出 A-1和B 或者 A与B-1 与 是否可以构成 C-1。。。

状态转移方程:

用f[i][j] 表示 表示A前 i 为 和B 前j 位是否可以组成 C的前i+j位        

        dp[i][j]= (dp[i-1][j]&&(a[i]==c[i+j]))||(dp[i][j-1]&&(b[j]==c[i+j]))

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<climits>
#include<iostream>
#include<queue>
#include<set>
#include<map>
using namespace std;
int t;
char a[210],b[210],c[420];
int dp[210][210],length_a,length_b,length_c;

int main(void)
{
int i,j;
cin>>t;
for(int ii=1;ii<=t;ii++)
{
scanf("%s%s%s",a,b,c);
//printf("%s\n%s\n%s\n",a,b,c);
memset(dp,0,sizeof(dp));//to be confirmed

length_a=strlen(a);
length_b=strlen(b);
length_c=strlen(c);
if(length_c!=length_a+length_b)
{
printf("Data set %d: no\n",ii);
continue;
}
dp[0][0]=1;
for(int i=1;i<=length_a;i++)
{
if(a[i-1]==c[i-1])
dp[i][0]=1;
else
break;
}
for(int j=1;j<=length_b;j++)
{
if(b[j-1]==c[j-1])
dp[0][j]=1;
else
break;
}
for(int i=1;i<=length_a;i++)
{
for(int j=1;j<=length_b;j++)
{
dp[i][j]=((a[i-1]==c[i+j-1])&&(dp[i-1][j]))||((b[j-1]==c[i+j-1])&&(dp[i][j-1]));
//printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
}
}
if(dp[length_a][length_b]==1)
{
printf("Data set %d: yes\n",ii);
}
else
{
printf("Data set %d: no\n",ii);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: