hdu1501&&poj2192 Zipper(DFS)

转载请注明出处:http://blog.csdn.net/u012860063

题目链接

HDU:http://acm.hdu.edu.cn/showproblem.php?pid=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 should be 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和字符串2的字母的顺序的情况下,是否能组成字符串3。

代码例如以下:

#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%s",a+1,b+1,c+1);//从字符串下标为1開始输入
			len1 = strlen(a+1);
			len2 = strlen(b+1);
			len3 = strlen(c+1);
			int flag = 0;
			if(c[len3]==a[len1] || c[len3]==b[len2])//优化,假设C最后的字母是a的最后一个
			{										//或者b的最后一个才有可能是由它们组成的
				flag = dfs(1, 1, 1);
			}
			if(flag == 1)
			printf("Data set %d: yes\n",++cas);
			else
			printf("Data set %d: no\n",++cas);
		}
	}
	return 0;
}
时间: 2024-10-01 05:28:21

hdu1501&amp;&amp;poj2192 Zipper(DFS)的相关文章

hdu1501&amp;&amp;poj2192 Zipper(DFS)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=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

hdu 1501 Zipper dfs

题目链接: HDU - 1501 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 examp

DP练习 Poj2192 Zipper

题目描述:http://poj.org/problem?id=2192 解题思路: 1.book[i][j]表示str2的前i个元素和str1的前j个元素是否能组成Str_Link的前(i+j)个元素所组成的数组: 如果是,则book[i][j]=1,否则,book[i][j]=0: 2.book[0][j]表示str1的前j个元素是否能组成Str_Link的前j个元素所组成的数组: book[i][0]表示str2的前i个元素是否能组成Str_Link的前i个元素所组成的数组; 3.状态转移方

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim

POJ动态规划练习(计划6-8题)

Problems POJ2192 - Zipper Solutions POJ2192 - Zipper 题目大意:给定字符串s1, s2和s,问s1和s2是否满足:均为s的子序列(不必连续)且恰能构成s? 用布尔变量 dp[i][j] 表示 s1 中前 i 个字符和 s2 中前 j 个字符能否构成 s 的前 i + j 个字符构成的子串 s',显然 s' 的最后一个字符只能是 s1[i] 和 s2[j] 中的一个,由此可以写出状态转移方程.边界条件为 dp[0][0] = 1. 1 // Pr

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

HDOJ 题目1501 Zipper(DFS)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7223    Accepted Submission(s): 2576 Problem Description Given three strings, you are to determine whether the third string can be formed

HDU 1501 Zipper(DP,DFS)

题意  判断能否由字符串a,b中的字符不改变各自的相对顺序组合得到字符串c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示能否有a的前i个字符和b的前j个字符组合得到c的前i+j个字符  值为0或者1  那么有d[i][j]=(d[i-1][j]&&a[i]==c[i+j])||(d[i][j-1]&&b[i]==c[i+j])   a,b的下标都是从1开始的  注意0的初始化 #include<cstdio> #include<cst

hdu1501 Zipper

Zipper Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 59   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Given three strings, you are t