题目传送

题目描述

Zipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12559 Accepted Submission(s): 4519

Problem 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

DFS和减枝(记忆性搜索),看一些大牛使用动规写的,功力实属不及,佩服

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>

using namespace std;
int T,la,lb,lc;
string s1,s2,s;
bool flag;
bool vis[220][220];
void DFS(int a,int b,int c)
{
	if(a==la&&b==lb&&c==lc)
	{
		flag=1;
		return ;
	}
	if(s1[a]!=s[c]&&s2[b]!=s[c]) 
	return ;
	if(vis[a][b])//做标记 嗯哼  搜索过了 
	return ;
	if(flag)
	return ;
	if(s1[a]==s[c])
	DFS(a+1,b,c+1);
	if(s2[b]==s[c])
	DFS(a,b+1,c+1);
	vis[a][b]=1;//表明已经搜索,打标记 
}
int main()
{
	int K=0;
	scanf("%d",&T);
	while(T--)
	{
		K++;
		cin>>s1>>s2>>s;
		la=s1.length();
		lb=s2.length();
		lc=s.length();
		flag=0;
		memset(vis,0,sizeof(vis));
		DFS(0,0,0);
		if(flag)
		cout<<"Data set "<<K<<": yes"<<endl;
		else
		cout<<"Data set "<<K<<": no"<<endl;
	}
	return 0;
} 

这个是动规写的,万物皆可 (动规)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>

using namespace std;
string a,b,c;
int dp[220][220];
int T,la,lb,lc;
int main()
{
	int K=0;
	cin>>T;
	while(T--)
	{
		K++;
		int i,j,k;
		cin>>a>>b>>c;
		la=a.length();
		lb=b.length();
		lc=c.length();
		memset(dp,0,sizeof(dp));
		dp[0][0]=1;
		for(i=0;i<=la;i++)
		{
			for(j=0;j<=lc;j++)
			{
				if(i>0&&c[i+j-1]==a[i-1]&&dp[i-1][j])
				dp[i][j]=1;
				if(j>0&&c[i+j-1]==b[j-1]&&dp[i][j-1])
				dp[i][j]=1;
			}
		}
		if(dp[la][lb])
		cout<<"Data set "<<K<<": yes"<<endl;
		else
		cout<<"Data set "<<K<<": no"<<endl;
	}
	return 0;
}

瞧不明白没关系,我一时半会也没有看明白