HDU 1501 Zipper(DFS)
2016-07-09 10:28
411 查看
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".
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女生赛类似的题
#include<iostream> #include<cstring> using namespace std; char a[205],b[205],c[405]; int l1,l2,l3; bool flag; int vis[205][205]; void dfs(int x,int y,int z) { if(flag) return; if(z==l3) { flag=true; return; } if(vis[x][y]==1) return; vis[x][y]=1; if(a[x]==c[z]) dfs(x+1,y,z+1); if(b[y]==c[z]) dfs(x,y+1,z+1); } int main() { int T,t; cin>>T; for(t=1;t<=T;t++) { cin>>a>>b>>c; l1=strlen(a); l2=strlen(b); l3=strlen(c); flag=false; memset(vis,0,sizeof(vis));//防止超时 if(l1+l2==l3) dfs(0,0,0); printf("Data set %d: ",t); if(flag)printf("yes\n"); else printf("no\n"); } return 0; }
相关文章推荐
- 文件操作
- 处理器在实施任务切换时的操作——《x86汇编语言:从实模式到保护模式》读书笔记39
- 小马哥--高仿三星G9200 主板型号A208刷机拆机主板图
- 浅谈C# 非模式窗体show()和模式窗体showdialog()的区别
- First Bad Version
- Centos6.7 编译安装 Apache PHP
- c++多线程
- python学习——Python基础
- mac os x 设置终端快捷键
- 归并排序(1)
- BZOJ2526: [Poi2011]Inspection
- Linux内核---49.i2s驱动分析1测试
- 简单工厂模式(Factory)
- 【钜惠预告】黑马程序员暑期抢座活动即将上线
- iOS开发出现 CUICatalog: Invalid asset name supplied: (null)
- 企业邮箱能带来什么
- 链表划分
- 虚析构函数
- Linux内核---48.spi驱动修改
- 编译链接中的-可重定位目标文件