HDOJ Zipper (DFS)
来源:互联网 发布:中国印度边境冲突 知乎 编辑:程序博客网 时间:2024/06/05 15:42
Zipper
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 1
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".
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: yesif the third string can be formed from the first two, or Data set n: noif it cannot. Of course n should 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
ac代码:
#include<stdio.h> #include<string.h> #include<iostream>#include<algorithm>using namespace std;char str1[205],str2[205],str3[405]; int bz; int len1,len2,len3; int v[205][205]; void dfs(int a,int b,int c) { if(bz) return; if(c==len3) { bz=1; return; } if(v[a][b]) //标记不能少 return; v[a][b]=1; if(str1[a]==str3[c]) dfs(a+1,b,c+1); if(str2[b]==str3[c]) dfs(a,b+1,c+1); } int main() { int i,t; int cas=0; scanf("%d",&t); while(t--) { scanf("%s%s%s",str1,str2,str3); printf("Data set %d: ",++cas); len1=strlen(str1); len2=strlen(str2); len3=strlen(str3); if(len1+len2!=len3) { printf("no\n"); continue; } memset(v,0,sizeof(v)); bz=0; dfs(0,0,0); if(bz) printf("yes\n"); else printf("no\n"); } return 0; }
0 0
- HDOJ Zipper (DFS)
- hdoj 1501 Zipper 【DFS + 剪枝】
- HDOJ 1501 Zipper(dfs)
- HDOJ 题目1501 Zipper(DFS)
- Zipper(dfs)
- hdoj 1501 Zipper
- hdoj 1501 Zipper
- DFS poj 2192 zipper
- 【hdu】Zipper (dfs)
- hdu 1501 Zipper (DFS)
- HDU 1501 Zipper(DFS)
- hdu1501&&poj2192 Zipper(DFS)
- HDU 1501 Zipper 【DFS】
- hdu dfs Zipper
- HDU 1501 Zipper(DFS)
- hdu1501 Zipper--DFS
- hdu 1501 Zipper(DFS)
- Hdu 1501 Zipper【dfs】
- 线程的三种创建方法
- 基本命令(按学习进度随机添加)
- ORM进阶之 ORM简介
- poj 2752 Seek the Name, Seek the Fame 【在原串中 找所有既是原串前缀又是原串后缀的子串长度】
- POj2516 Minimum Cost 最小费用最大流
- HDOJ Zipper (DFS)
- style-visibility
- java 深入解析FileInputStream和FileOutputStream
- java 序列化与反序列化
- 杭电 2087 剪花布条 kmp水题
- c++/c中流的概念
- UVa 10806 Dijkstra, Dijkstra (无向图+最小费用最大流+最短路:)
- HDOJ 5348 MZL's endless loop
- hdu4124