ZipperTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11498 Accepted Submission(s): 4130 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.
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.
Output For each data set, print:
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
|
以前不知道我想的那种写法居然是记忆化搜索咳咳再次写一遍的时候,才发现
# include <iostream>
# include <cstring>
# include <string>
using namespace std;
string s1, s2, s3;
bool flag, book[1500][1500];
void dfs(int str1, int str2, int str3)
{
if(flag)
{
return ;
}
if(str1 == s1.size() && str2 == s2.size())
{
flag = true;
return ;
}
if(s1[str1]!= s3[str3] && s2[str2] != s3[str3])
{
return ;
}
if(book[str1][str2])//标记已经搜过的地方就不搜了
{
return ;
}
book[str1][str2] = true;
if(s1[str1] == s3[str3])
{
dfs(str1 + 1, str2, str3 + 1);
}
if(s2[str2] == s3[str3])
{
dfs(str1 , str2 + 1, str3 + 1);
}
}
int main(int argc,char *argv[])
{
int n;
while(cin >> n)
{
for(int i = 0; i < n; i++)
{
flag = false;
memset(book, false, sizeof(book));
cin >> s1 >> s2 >> s3;
dfs(0, 0, 0);
if(flag)
{
cout << "Data set "<< i + 1 << ": " << "yes" << endl;
}
else
{
cout << "Data set "<< i + 1 << ": " << "no" << endl;
}
}
}
return 0;
}