题目链接: 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 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.
题意描述:给出三个字符串,在前两个字符串不改变自身相对顺序的前提下,判断第三个字符串是否由前两个字符串组成。
算法分析:dfs,优化一:如果第三个字符串的第一个和最后一个字符分别不是第一个或第二个字符串的首字母、末尾字母,那么肯定是不可能的;优化二:dfs过程中标记一个数组,判断此时状态是否访问过。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; int flag;
char s[maxn],s2[maxn],s3[maxn+maxn];
int vis[maxn][maxn];
int cnt,cnt2,cnt3,len,len2,len3; void dfs(int sum,int x,int y)
{
if (sum >= len3) {flag=;return ;}
if (s[x]!=s3[sum] && s2[y]!=s3[sum])
return ;
if (vis[x][y]) return ;
vis[x][y]=;
if (s[x]==s3[sum]) dfs(sum+,x+,y);
if (s2[y]==s3[sum]) dfs(sum+,x,y+);
return ;
} int main()
{
int t,ncase=;
while (scanf("%d",&t)!=EOF)
{
while (t--)
{
scanf("%s%s%s",s,s2,s3);
len=strlen(s);
len2=strlen(s2);
len3=strlen(s3);
flag=;
memset(vis,,sizeof(vis));
if (s3[len3-]==s[len-]||s3[len3-]==s2[len2-])
dfs(,,);
if (flag) printf("Data set %d: yes\n",ncase++);
else printf("Data set %d: no\n",ncase++);
}
}
return ;
}

hdu 1501 Zipper dfs的更多相关文章

  1. (step4.3.5)hdu 1501(Zipper——DFS)

    题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...

  2. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  3. HDU 1501 Zipper(DP,DFS)

    意甲冠军  是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...

  4. HDU 1501 Zipper(DFS)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

  5. hdu 1501 Zipper

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...

  6. HDU 1501 Zipper 动态规划经典

    Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. HDU 1501 Zipper 字符串

    题目大意:输入有一个T,表示有T组测试数据,然后输入三个字符串,问第三个字符串能否由第一个和第二个字符串拼接而来,拼接的规则是第一个和第二个字符串在新的字符串中的前后的相对的顺序不能改变,问第三个字符 ...

  8. hdu 1501 Zipper(DP)

    题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...

  9. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

随机推荐

  1. Python学习-前台开发-JavaScript、Dom和jQuery

    JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. ...

  2. springboot10 framwork

    一.Spring介绍 Spring 是位于业务逻辑层的框架. 优点很多(无缝对接前后层的框架.提供AOP的支持 , 和以前的 Sstruts . Hibernate 组合成了一套框架组合 SSH .现 ...

  3. git使用及一些配置、问题

    安装https://git-for-windows.github.io/ 一.绑定用户名.邮件地址 git config --global user.name "Your Name" ...

  4. Linux networkmanager

    我们开发的网络,出于保密,只能叫XXX网络,或者我更倾向于称之为WTF-network 由于经常处于封闭的环境,刚一接触新一点的世界,总是有那么一点猝不及防.最近发现配置的静态路由经常消失,经发现是n ...

  5. lombok 去除麻烦的实体类get和set,toString书写

    首先在pom.xml中添加 <dependency> <groupId>org.projectlombok</groupId> <artifactId> ...

  6. 【bzoj2044】三维导弹拦截 dp+二分图最大匹配

    题目描述 n个物品,第i个位置有ai.bi.ci三种属性.每次可以选出满足$\ a_{p_i}<a_{p_{i+1}}\ ,\ b_{p_i}<b_{p_{i+1}}\ ,\ c_{p_i ...

  7. 2-sat 学习笔记

    一.问题描述 以你咕的模板题为例 题目描述 有\(n\)个布尔变量\(x_1\)~\(x_n\),另有\(m\)个需要满足的条件,每个条件的形式都是"\(x_i\)为true/false或\ ...

  8. js 数组遍历

    map.filter.forEach.every.some http://www.runoob.com/jsref/jsref-obj-array.html 1.在字符串中使用 map 在一个Stri ...

  9. hihoCoder 1015KMP

    #include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> ...

  10. YYH的球盒游戏(NOIP模拟赛Round 6)

    题目描述 YYH有一些总共有种颜色的球,他有颜色的球个.他同样有个盒子,第个盒子能放个球. 他的目标是把这个球按规则放进个盒子里: 对于一个盒子,对于每种颜色的球至多只能放个. 把颜色为的球放进盒子, ...