HDU 1501 Zipper(DFS)
Problem Description
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
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
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
#include<iostream>
#include<cstring>
using namespace std;
char a[],b[],c[];
int l1,l2,l3;
bool flag;
int vis[][];
void dfs(int x,int y,int z)
{
if(flag)
return;
if(z==l3)
{
flag=true;
return;
}
if(vis[x][y]==)
return;
vis[x][y]=;
if(a[x]==c[z])
dfs(x+,y,z+);
if(b[y]==c[z])
dfs(x,y+,z+);
}
int main()
{
int T,t;
cin>>T;
for(t=;t<=T;t++)
{
cin>>a>>b>>c;
l1=strlen(a);
l2=strlen(b);
l3=strlen(c);
flag=false;
memset(vis,,sizeof(vis));//防止超时
if(l1+l2==l3)
dfs(,,);
printf("Data set %d: ",t);
if(flag)printf("yes\n");
else printf("no\n");
}
return ;
}
HDU 1501 Zipper(DFS)的更多相关文章
- hdu 1501 Zipper(DP)
题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- HDU 5965 扫雷(dfs)题解
题意:给你一个3*n的格子,中间那行表明的是周围8格(当然左右都没有)的炸弹数量,上下两行都可以放炸弹,问你有几种可能,对mod取模 思路:显然(不),当i - 1和i - 2确定时,那么i的个数一定 ...
- HDU 1518 Square(DFS)
Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...
- HDU 1015 Safecracker (DFS)
题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...
- Hdu 1175 连连看(DFS)
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...
- HDU1501 Zipper(DFS) 2016-07-24 15:04 65人阅读 评论(0) 收藏
Zipper Problem Description Given three strings, you are to determine whether the third string can be ...
- 【OpenJ_Bailian - 2192】Zipper(dfs)
Zipper Descriptions: Given three strings, you are to determine whether the third string can be forme ...
- hdu 2821 Pusher (dfs)
把这个写出来是不是就意味着把 http://www.hacker.org/push 这个游戏打爆了? ~啊哈哈哈 其实只要找到一个就可以退出了 所以效率也不算很低的 可以直接DFS呀呀呀呀 ...
随机推荐
- 工控机安装Ubuntu14.04
开机,不停按delete,进入bios 进入boot,选择USB启动 重新开机,进入安装向导,下一步即可
- python 散列表查找
class HashTable: def __init__(self, size): self.elem = [None for i in range(size)] self.count = size ...
- PHP获取固定概率
public static function getFixRand($weight) { if ((int)$weight < 0 || (int)$weight > 100) { ret ...
- MongoDB(课时19 数据删除)
3.4.4 删除数据 在MongoDB里面删除数据使用“remove()”.但是这个函数有两个可选项: 删除条件:满足条件的数据被删除. 只删除一个数据:设置为true或者是1表示只删除一个. 范例: ...
- 《剑指offer》第三十五题(复杂链表的复制)
// 面试题35:复杂链表的复制 // 题目:请实现函数ComplexListNode* Clone(ComplexListNode* pHead),复 // 制一个复杂链表.在复杂链表中,每个结点除 ...
- Python -- Scrapy 架构概览
架构概览 本文档介绍了Scrapy架构及其组件之间的交互. 概述 接下来的图表展现了Scrapy的架构,包括组件及在系统中发生的数据流的概览(绿色箭头所示). 下面对每个组件都做了简单介绍,并给出了详 ...
- LeetCode--066--加一
问题描述: 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. ...
- UVA-1533 Moving Pegs (路径寻找问题)
Description Venture MFG Company, Inc. has made a game board. This game board has 15 holes and thes ...
- 贪心(二)NYOJ14题
#include <iostream> #include<cmath> #include "algorithm" using namespace std; ...
- uinex 常用命令
删除文件夹实例:rm -rf /var/log/httpd/access将会删除/var/log/httpd/access目录以及其下所有文件.文件夹 删除文件使用实例:rm -f /var/log/ ...