Hdoj 1501 Zipper (DFS)

Source: Internet
Author: User
Tags strlen
Zipper

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7781 Accepted Submission (s): 2754


Problem Description Given Three strings, you is to determine whether the third string can is formed by combining the Char Acters 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".

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, 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 After this problem, the recursive backtracking in the search finally see the point. The recursive function is the one that crashes most of the search code. The recursive return value was previously assumed to return directly to the main function, ending the recursion. Today, the recursive function code that debugs this problem finds that the return value in recursion is mostly returned to the true and false value of the last call to the function. Recursion is a backtracking process, constantly approaching the target, and then pulling out the target step by side, constantly through the function of the statement operations, to obtain the required value of the topic. (I do not know that it is right, even if the estimation of other people also do not understand the next year's learning younger sister, see do not laugh at me, this slag to try to) the topic: Give three strings, the third by the first two free combination, but can not change the letter in the original string in the front and back position.   Write a program to determine whether the given three strings meet the above requirements. The title is not difficult, at least better than the maze, the code is as follows:
#include <stdio.h>
#include <string.h>
char a[210],b[210],c[410];
int V[210][210],lena,lenb,lenc;
int dfs (int i,int j,int k)
{
	if (c[k]== ')
	   return 1;
	if (V[i][j])//each position can only be used once, do not perform this step pruning will time out 
	   return 0;
	V[i][j]=1;
	if (A[i]==c[k]&&dfs (i+1,j,k+1))
	    return 1;
	if (B[j]==c[k]&&dfs (i,j+1,k+1))
	    return 1;
	The letters in return 0;//A,B cannot be found c[k] on end find 
} 

int main ()
{
	int t,n=1;
	scanf ("%d", &t);
	while (t--)
	{
		memset (v,0,sizeof (v));
		scanf ("%s%s%s", a,b,c);
		Lena=strlen (a);
		Lenb=strlen (b);
		Lenc=strlen (c);
		printf ("Data Set%d:", n);
		if (Dfs (0,0,0))
		    printf ("yes\n");
		else
		    printf ("no\n");
		n++;
	}
	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.