HDU 1501 Zipper (DFS)

Source: Internet
Author: User

Zipper

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)

Total submission (s): 8580 Accepted Submission (s): 3028

Problem Description

Given three strings, you is to determine whether the third string can is formed by combining the characters in the first The strings. The first and the strings can is mixed arbitrarily, but each must stay on its original order.

For example, consider forming "tcraete" from "Cat" and "tree":

String A:cat

String B:tree

String C:tcraete

As can see, we can form the third string by alternating characters from the 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's impossible to form "cttaree" from "Cat" and "tree".

Input

The first line of input contains a, 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, and one data set per line.

For each data set, the line of input consists of three strings, separated by a single space. All strings is composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first and the strings. The first and strings would have lengths between 1 and characters, inclusive.

Output

For each data set, print:

Data Set N:yes

If the third string can be formed from the first, or

Data Set N:no

If it cannot. Of course n should is 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

Source

Pacific Northwest 2004

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intmaxn=1010;6 BOOLV[MAXN][MAXN];7 intf=0;8 intL1,l2,l3;9 CharA[MAXN],B[MAXN],C[MAXN];Ten voidDfsintX1,intX2,intx3) One { A     if(x3>=L3) { -f=1; -         return ; the     } -     if(f==1)return ; -     if(V[x1][x2])return ; -     if(A[x1]==c[x3]) DFS (x1+1, x2,x3+1); +     if(B[x2]==c[x3]) DFS (x1,x2+1, x3+1); -v[x1][x2]=1; + } A intMain () at { -     intN; -Cin>>N; -      for(intI=1; i<=n;i++){ -cout<<"Data Set"<<i<<": "; -scanf"%s%s%s", a,b,c); inmemset (V,0,sizeof(v)); -l1=strlen (a); toL2=strlen (b); +l3=strlen (c); -         if(l1+l2!=L3) { thecout<<"No"<<Endl; *             Continue; $         }Panax Notoginseng         Else{ -f=0; theDfs0,0,0); +             if(f==0) cout<<"No"<<Endl; A             Elsecout<<"Yes"<<Endl; the         } +     } -     return 0; $}

HDU 1501 Zipper (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.