题目链接: 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. PEAR DB 初学笔记

    1.数据查询 i. DB_common::getAll() DB_FETCHMODE_ORDERED . DB_FETCHMODE_ASSOC . DB_FETCHMODE_OBJECT ii. DB ...

  2. 计算机概念总结5-阿里云的了解2-slb

    https://help.aliyun.com/document_detail/27539.html?spm=a2c4g.11186623.6.544.3c3c5779UdHKeO 概述 负载均衡(S ...

  3. Linux下samba编译与安装(Ubuntu和嵌入式linux)

    Ubuntu[i386-linux下安装过程] 1.安装samba $ sudo apt-get install samba $ sudo apt-get install smbfs (可选) $ s ...

  4. [转]Shell dev-null详解

    转自: https://blog.csdn.net/wenwenxiong/article/details/46882733 1,可以将/dev/null看作"黑洞". 它非常等价 ...

  5. FormsAuthentication类

    理解代码: string cookieName = FormsAuthentication.FormsCookieName; FormsAuthentication类说明: // 摘要: // 为 W ...

  6. finally代码块不被执行的情况总结

    以前跟别人讨论finally关键字,我总是简单的说:“fianly代码块中的代码一定会执行,一般用来清除IO资源等非内存资源(内存资源由GC机制回收)”. 今天翻书发现了几种不会执行的情况,现在总结下 ...

  7. hdu 1534 Schedule Problem (差分约束)

    Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. synflood 模拟工具

    synflood 模拟工具 来源 https://blog.csdn.net/wuzhimang/article/details/54581117 因项目需要,要对主流的几家抗DDoS设备做测评,当然 ...

  9. 牛客 2018NOIP 模你赛2 T2 分糖果 解题报告

    分糖果 链接:https://www.nowcoder.com/acm/contest/173/B 来源:牛客网 题目描述 \(N\) 个小朋友围成一圈,你有无穷个糖果,想把其中一些分给他们. 从某个 ...

  10. js常用数组去重

    // ES6 function unique (arr){ const seen = new Map() return arr.filter((a) => !seen.has(a) && ...