题目链接: 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网络编程(http协议,IO多路复用、select内核监听)

    前言: 什么是IO? 分为IO设备和IO接口两个部分 如Linux系统,I/O操作可以有多种方式 比如DIO(DirectI/O) AIO(AsynchronousI/O异步I/O) Memory-M ...

  2. cloud-utils

    官方下载:https://launchpad.net/cloud-utils rpm包下载地址:http://rpmfind.net/linux/rpm2html/search.php?query=c ...

  3. 系统编程--高级IO

    1.非阻塞I/O 非阻塞I/O使我们可以调用不会永远阻塞的I/O操作,例如open,read和write.如果这种操作不能完成,则立即出错返回,表示该操作如继续执行将继续阻塞下去.对于一个给定的描述符 ...

  4. UVa 11806 - Cheerleaders (组合计数+容斥原理)

    <训练指南>p.108 #include <cstdio> #include <cstring> #include <cstdlib> using na ...

  5. Horn–Schunck 光流法与其算法理解(gup cuda)

    1. 基于Horn-Schunck模型的光流算法 1.1     光流的约束条件 光流 的假设条件认为图像序列,在时间t 的某一像素点与在时间t+1的这一像素点的偏移量保持不变,即 .这就是灰度值守恒 ...

  6. Nginx日志管理配置

    1.创建日志目录 nginx 的默认日志目录所在硬盘空间可能比较小,所以根据硬盘的空间状况创建日志目录 例如:mkdir /backup/nginx_logs 2.修改nginx配置文件 配置 ngi ...

  7. 可以在函数中间打点了,以分析bpf_prog_load函数为例

    可以在函数中间打点了, sudo stap -L 'process("./test").statement("func@test.c:10")' //12.10 ...

  8. 二、vue响应式对象

    Object.defineProperty Object.defineProperty 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象,先来看一下它的语法: Obj ...

  9. BZOJ 2243 染色(树链剖分好题)

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 7971  Solved: 2990 [Submit][Stat ...

  10. [UOJ#131][BZOJ4199][NOI2015]品酒大会

    [UOJ#131][BZOJ4199][NOI2015]品酒大会 试题描述 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个 ...