HDU 1501 Zipper (DFS)

Source: Internet
Author: User

HDU 1501 Zipper (DFS)
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 shocould 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: yesData set 3: no

 

Question: Give you three words. You need to determine whether the third word is composed of the first two words in any order (the order of the two words cannot be changed ).

Idea: the data is too small. Simply use DFS! Use a and B to indicate which letter the two words appear. If the two words can finally appear, the conditions are met.

 

AC code:

 

#include
 
  #include
  
   #includeusing namespace std;char str1[205];char str2[205];char str[405];int vis[205][205];int len1,len2,len;int flag;void dfs(int a,int b,int l){    if(vis[a][b])        return ;    if(a==len1&&b==len2)    {        flag=1;    }    if(flag==1)        return ;    vis[a][b]=1;    if(str[l]==str1[a]&&a<len1) {="" a++;="" dfs(a,b,l+1);="" a--;="" }="" if(str[l]="=str2[b]&&b
   
    

 

Related Article

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.