题目
2192:Zipper
查看提交统计提示提问
总时间限制: 1000ms 内存限制: 65536kB
描述
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”.
输入
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.
输出
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.
样例输入
3
cat tree tcraete
cat tree catrtee
cat tree cttaree
样例输出
Data set 1: yes
Data set 2: yes
Data set 3: no
注意String C是由String A&String B组成才成立!而且具有顺序
方法一:
#include <bits/stdc++.h>
using namespace std;
int mark[300][300];
char a[300],b[300],c[500];
int la,lb,lc;
int dfs(int i,int j,int k){
if(c[k]=='\0')return 1;
if(mark[i][j])return 0;//避免重复查找,导致超时
mark[i][j]=1;//标记已经找过的位置
if(la<lc)
if(a[i]==c[k]&&dfs(i+1,j,k+1))return 1;
if(lb<lc)
if(b[j]==c[k]&&dfs(i,j+1,k+1))return 1;
return 0;
}
int main(){
int n,num=0;
scanf("%d",&n);
while(n--){
scanf("%s%s%s",a,b,c);
la=strlen(a);
lb=strlen(b);
lc=strlen(c);
memset(mark,0,sizeof(mark));//必要
if(dfs(0,0,0)==1)printf("Data set %d: yes\n",++num);
else printf("Data set %d: no\n",++num);
}
return 0;
}
方法二:
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <stack>
//#include<bits/stdc++.h>
using namespace std;
const int nn=220;
string s1,s2,s3;
int dp[nn][nn];//第一个存s1长度下判断是否与s3相等,第二个存s2……
int main() {
int t;
scanf("%d",&t);
for(int n=1;n<=t;n++){
cin>>s1>>s2>>s3;
memset(dp,0,sizeof(dp));//每次查找前先置零
dp[0][0]=1;//初始化,为了后面循环判断字符串之间是否相等所以 先将它们均为0的位置置1
int len1=s1.size(),len2=s2.size();
for(int i=0;i<=len1;i++){
for(int j=0;j<=len2;j++){
if(i<len1&&s1[i]==s3[i+j]&&dp[i][j])dp[i+1][j]=1;
if(j<len2&&s2[j]==s3[i+j]&&dp[i][j])dp[i][j+1]=1;
}
}
// for(int i=0;i<=len1;i++){
// for(int j=0;j<=len2;j++){
// printf("%d ",dp[i][j]);
// }cout<<endl;
// }cout<<endl;
//若答案为1则说明符合要求
if(dp[len1][len2])printf("Data set %d: yes\n",n);
else printf("Data set %d: no\n",n);
}
return 0;
}
若不理解我们可以通过看dp数组里的值来自己推一推下面放个例题数据的dp内数据