Problem Description
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.

 
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
首先说关于字符串匹配寻找的基本思想  将 a b两个数字的位数依次和c中的比较  如果匹配合适的话 a/b 以及c的坐标前移一位 搜索的终点为字符串的结尾‘\0’
这里用的dfs的参数有三个  分别为a b c三个数组的坐标(当a b的某位和c相同的时候 便出现了分支 也正是由于分支的存在 所以标记数组就可以出现用来剪枝啦)
由于到达坐标 i j(由于c数组的长度为 a b之和 所以k没有必要作为参数 一个二维数组就可以搞定了) 的情况有很多种 我们的目标是去匹配字符  当坐标为i j的时候
a b c剩下的字符都是一样的 所以不论怎么匹配到 i j 情况都是一样的 为了避免超时  这里就可以用一个标记数组进行记忆搜索啦
贴上代码
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
string a,b,c;
int vis[501][500],flag;
void dfs(int i,int j,int k)
{
 if(vis[i][j]) return;// 对标记过的次数直接忽视
 vis[i][j]=1; // 标记
 if(c[k]=='\0')
 {
   flag=1;
   return; 
 }
 if(a[i]==c[k]&&b[j]==c[k])
 {
  dfs(i+1,j,k+1);
  dfs(i,j+1,k+1);
 }else if(a[i]==c[k]&&b[j]!=c[k])
 {
  dfs(i+1,j,k+1);
 }else if(a[i]!=c[k]&&b[j]==c[k])
 {
  dfs(i,j+1,k+1);
 }
}
int main()
{
 int n,i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
     cin>>a>>b>>c;
     flag=0;
     memset(vis,0,sizeof(vis));
        dfs(0,0,0);
        printf("Data set %d: ",i);
  if(flag==1) cout<<"yes"<<endl;
  else cout<<"no"<<endl;     
    }
 return 0;
}

hdu1501 记忆化搜索。。。的更多相关文章

  1. hdu1501 记忆化搜索

    题意:       给你三个字符串,问你前两个能不能拼成第三个串. 思路:       直接记忆化神搜就行,思路水,看下代码就知道了.这个题目我感觉最大公共子序列dp的作法是错的,虽然有人ac了,随便 ...

  2. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

  3. 【BZOJ-3895】取石子 记忆化搜索 + 博弈

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 263  Solved: 127[Submit][Status][Discuss] D ...

  4. hdu3555 Bomb (记忆化搜索 数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  5. zoj 3644(dp + 记忆化搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...

  6. loj 1044(dp+记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...

  7. DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects

    题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...

  8. HDU1978 记忆化搜索

    How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. bzoj4562: [Haoi2016]食物链--记忆化搜索

    这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...

随机推荐

  1. phpstorm 断点调试总是从index.php的第一行开始

    去掉勾选,重开phpstorm

  2. c代码审查软件

    1. Coccinelle http://coccinelle.lip6.fr/

  3. percona mysql5.7进程出现大量unauthenticated user解决记录

    现象:http://task.chinasoft.com.wx/ 所有任务能打开,我的任务打开很慢 有些人能用,有些人不能用,数据库出现大量的未认证用户连接,连接数利用率超过70% 重启nginx,a ...

  4. ROS学习笔记(二)

    ===================================================== QT工具箱sudo apt-get install ros-kinetic-rqtsudo ...

  5. Spring cloud微服务安全实战-3-10API安全机制之授权

    说一下最后一个模块,授权.用来做访问控制,控制哪个用户能干什么.哪个用户不能干什么? 遵循最小的授权原则,一个用户只给他必须要的那些权限. 1.你的请求是不是需要权限认证, 有一些请求是根本不需要权限 ...

  6. k8s记录-下载k8s相关二进制包(一)

    1)软件清单操作系统:CentOS7Kubernetes版本:v1.14.2flannel:v0.10.0ectd3:v3.3.11Docker版本:v18.09.0-ceDocker-compose ...

  7. Linux下压力测试命令ab

    ab命令被集成到了httpd服务器中,所以想要使用ab命令需要先安装httpd服务.yum -y install httpd (1).ab命令的使用方法和常用选项 ab [选项] [http[s]:/ ...

  8. mybatis 存储过程与游标的使用

    MyBatis还能对存储过程进行完全支持,这节开始学习存储过程.在讲解之前,我们需要对存储过程有一个基本的认识,首先存储过程是数据库的一个概念,它是数据库预先编译好,放在数据库内存中的一个程序片段,所 ...

  9. 【源码解析】Flink 是如何基于事件时间生成Timestamp和Watermark

    生成Timestamp和Watermark 的三个重载方法介绍可参见上一篇博客: Flink assignAscendingTimestamps 生成水印的三个重载方法 之前想研究下Flink是怎么处 ...

  10. 最基础的Python的socket编程入门教程

    最基础的Python的socket编程入门教程 本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在 ...