题目链接: 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. Box布局管理

    创建wx.BoxSizer对象时可以指定布局方向: hbox = wx.BoxSizer(wx.HORIZONTAL) 设置为水平方向 hbox = wx.BoxSizer() 默认就是就是水平方向的 ...

  2. DataSource的设置

    1.Centos和redhat,Fedora等版本无须设置,直接在cloud.cfg指定,默认是EC2 datasource_list: ['ConfigDrive','OpenStack'] 2.u ...

  3. 使用Cookie保存用户和密码然后自动登录

    login.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  4. 使用Bootstrap框架的HTML5页面模板

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. CSS绘制三角形的原理剖析

    今天学习Bootstrap时候,看到按钮的向下三角形源码: .caret { display: inline-block; ; ; margin-left: 2px; vertical-align: ...

  6. NetScaler Active-Active模式

    NetScaler Active-Active模式 NetScaler Active-Active模式 (此文档基于版本:NS9.3: Build 55.6 nc) By ShingTan Activ ...

  7. vs2008升级正式版

    1.VS2008简体中文正式版序列号 1.Visual Studio 2008 Professional Edition: XMQ2Y-4T3V6-XJ48Y-D3K2V-6C4WT 2.Visual ...

  8. Linux中权限(r、w、x)对于目录与文件的意义

    Linux中权限(r.w.x)对于目录与文件的意义 一.权限对于目录的意义 1.首先要明白的是目录主要的内容是记录文件名列表和子目录列表,而不是实际存放数据的地方. 2.r权限:拥有此权限表示可以读取 ...

  9. andrio

    ## 以eclipse -clean命令从命令行启动 eclipse ## 配置Android模拟器 点击上图右边的按钮(像个手机一样的),打开AVD管理器后,点击 New 新建一个模拟器,输入Nam ...

  10. lxml.html删除节点树和tag对

    # encoding: utf-8import StringIO from apihelper import info, info_savefrom lxml import etree, htmlfr ...