Hdu1501 & poj2192 zipper (DFS)

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question Link

HDU: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1501

Poj: http://poj.org/problem? Id = 2192


Zipper

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

Source

Pacific Northwest 2004

The question is whether string 3 can be formed without changing the character sequence of string 1 and string 2;


The Code is as follows:

# Include <cstdio> # include <cstring> # define n 247 char a [n], B [N], C [n + N]; int len1, len2, len3; int DFS (int x, int y, int sum) {If (sum> len3) return 1; if (a [x]! = C [Sum] & B [y]! = C [Sum]) return 0; if (a [x] = C [Sum] & DFS (x + 1, Y, sum + 1) return 1; if (B [y] = C [Sum] & DFS (X, Y + 1, sum + 1) return 1; return 0 ;} int main () {int T, CAS = 0; while (~ Scanf ("% d", & T) {While (t --) {scanf ("% S % s", A + 1, B + 1, c + 1); // input len1 = strlen (a + 1) from the string subscript of 1; len2 = strlen (B + 1 ); len3 = strlen (C + 1); int flag = 0; If (C [len3] = A [len1] | C [len3] = B [len2]) // optimized. If the last letter of C is the last {// of A or the last letter of B, it may be the flag consisting of them = DFS (1, 1, 1);} If (flag = 1) printf ("Data Set % d: Yes \ n", ++ CAS); elseprintf ("Data Set % d: no \ n ", ++ CAS) ;}} return 0 ;}



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.