SOJ 1010

1010. Zipper

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

利用动态规划的思路,若A,B能构成C,则C的最后一位必定为A的最后一位或者B的最后一位,若假定为A的最后一位,只要验证A-1和B能否构成C-1及A的最后一位与C的是否相等;若假定为B的最后一位,只要验证A和B-1能否构成C-1及B的最后一位与C的是否相等.若均不成立,则A和B不能构成C。

用dp[i][j]的值来表示A的前i位和B的前j位能否构成C的前i+j位,依次推出dp[lenA][lenB]的值,即可确定A和B能否构成C。

// Problem#: 1010
// Submission#: 4933146
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>

#include<string>

using namespace std;

string A,B,C;
int lenA,lenB,lenC;

int dp[201][201];//dp[i][j]用来表示A的前i位和B的前j位能否构成C的前i+j位 
int main()
{
    
    int n;
    cin>>n;
    dp[0][0]=1;
    for(int i=0;i<n;++i)
    {
        cin>>A>>B>>C;
        lenA=A.size();
        lenB=B.size();
        lenC=C.size(); 
        for(int j=1;j<=lenA;++j)
        {
            dp[j][0]=dp[j-1][0]&&(A[j-1]==C[j-1]);//记录A的前j位能否构成C的前j位,算出边界条件 
        }
        for(int j=1;j<=lenB;++j)
        {
            dp[0][j]=dp[0][j-1]&&(B[j-1]==C[j-1]);//记录B的前j位能否构成C的前j位,算出另一个边界条件 
        }
        for(int j=1;j<=lenA;++j)
        {
            for(int k=1;k<=lenB;++k)
            {
                dp[j][k]=(dp[j-1][k]&&(A[j-1]==C[j+k-1]))||(dp[j][k-1]&&(B[k-1]==C[j+k-1]));//计算A的前j位和B的前k位能否构成C的前j+k位。 
            }//共两种情况,即C的前j+k位的最后一位来自A,或者最后一位来自B,只要两种情况任意一种成立,则dp[j][k]为1 
        }
        if(dp[lenA][lenB])//即C能由A和B构成 
        cout<<"Data set "<<i+1<<": yes"<<endl;
        else
        cout<<"Data set "<<i+1<<": no"<<endl;
         
    }
    return 0;
}                                 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值