2016搜索提高1010

来源:互联网 发布:我的淘宝订单 编辑:程序博客网 时间:2024/06/05 03:20

Zipper

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 93   Accepted Submission(s) : 40

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

3cat tree tcraetecat tree catrteecat tree cttaree

Sample Output

Data set 1: yesData set 2: yes

Data set 3: no

思路:直接暴力搜索即可,标记搜索的地方;

代码:

#include <iostream>#include <stdio.h>#include <string.h>#include <cmath>#include <queue>#include <stdlib.h>#include <set>#define pi acos(-1.0)using namespace std;char a[201],b[201],c[402];int m,n,k;int e[202][202];int dfs(int x,int y,int z){    if(z==k)return 1;    if(e[x][y])return 0;    e[x][y]=1;    if(c[z]==a[x]&&dfs(x+1,y,z+1))return 1;    if(c[z]==b[y]&&dfs(x,y+1,z+1))return 1;    return 0;}int main(){    int t;    cin>>t;    int p=1;    while(t--)    {        cin>>a>>b>>c;        m=strlen(a);        n=strlen(b);        k=strlen(c);        memset(e,0,sizeof(e));        printf("Data set %d: ",p);        if(dfs(0,0,0))        {            cout<<"yes"<<endl;        }        else cout<<"no"<<endl;        p++;    }    return 0;}

0 0
原创粉丝点击