Zipper (p2192)

来源:互联网 发布:安可公关 知乎 编辑:程序博客网 时间:2024/05/22 01:51

这个题开始还是不太知道怎么做,,还是状态最重要啊。。先知道一点状态的变化就OK了,

dp[i][j]内保存的是用前一个字符串的前i个字符和第二个字符的前j个字符能构成后面的字符串的前i+j个字符。


#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<set>#include<cstdlib>#include<cstring>#include<stack>#include<string>using namespace std;//freopen("C://i.txt","r",stdin);int n;char a[222],b[222],c[444];int dp[222][222];int main(){freopen("C://i.txt","r",stdin);int i,j,k;int num=0;cin>>n;while (n--){num++;memset(dp,0,sizeof(dp));dp[0][0]=1;cin>>a>>b>>c;int lena=strlen(a);int lenb=strlen(b);int lenc=strlen(c);for (i=0;i<lena;i++){if (a[i]==c[i])dp[i+1][0]=1;elsebreak;}for (i=0;i<lenb;i++){if (b[i]==c[i]){dp[0][i+1]=1;}elsebreak;}for (i=1;i<=lena;i++){for (j=1;j<=lenb;j++){if (dp[i-1][j]&&a[i-1]==c[i+j-1]){//cout<<i<<' '<<j<<endl;dp[i][j]=1;}if (dp[i][j-1]&&b[j-1]==c[i+j-1]){//cout<<i<<' '<<j<<endl;dp[i][j]=1;}}}cout<<"Data set "<<num<<": ";if (dp[lena][lenb])cout<<"yes"<<endl;elsecout<<"no"<<endl;}} 



Zipper
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12998 Accepted: 4544

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

3cat tree tcraetecat tree catrteecat tree cttaree

Sample Output

Data set 1: yesData set 2: yesData set 3: no