HDU 1501 Zipper Memory Search

Source: Internet
Author: User


ZipperTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7188 Accepted Submission (s): 2571


Problem Descriptiongiven Three strings, you is to determine whether the third string can is formed by combining the Chara Cters in the first and 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".

Inputthe 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, 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.


Outputfor 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
3cat Tree tcraetecat Tree catrteecat tree Cttaree

Sample Output
Data set 1:yesdata set 2:yesdata set 3:no



Test instructions: Enter a string of three, aa,bb,cc. CC is made up of AA and BB. However, the original order of AA and BB in CC does not change. Ask if CC can be composed of AA and BB.


Procedure: DFS, to record the status. Take the current letter of AA to try, can match CC's current letter, continue to search down. If not, search for the current letter of the BB and CC.



#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> char aa[1010],bb[1010],cc[1010];int dp[1010][1010];int La,lb,lc;int dfs (int ta,int tb,int tc) {if (dp[ta][tb]!=-1) return dp[ta][tb];if (TC==LC) return 1; if (TA!=LA&AMP;&AMP;AA [TA]==CC[TC]) &&dfs (ta+1,tb,tc+1)) return 1;if ((TB!=LB&AMP;&AMP;BB[TB]==CC[TC)) &&dfs (ta,tb+1,tc+1 )) return 1;return dp[ta][tb]=0;} int main () {int n;scanf ("%d", &n), for (int cas=1;cas<=n;cas++) {scanf ("%s%s%s", AA,BB,CC); La=strlen (AA); lb= Strlen (BB); Lc=strlen (CC); memset (dp,-1,sizeof dp), if (La+lb==lc&&dfs (0,0,0)) printf ("Data set%d:yes\n", CAs); elseprintf ("Data set%d:no \ n ", CAs);}  Cin>>n;return 0;}






HDU 1501 Zipper Memory Search

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.