这题有两种解题思路,一个是记忆化搜索,一个是dp。

分别贴代码:

记忆化搜索:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
char str[],a[],b[];
int sum,flag;
int hash[][];
void dfs(int i,int j,int k)
{
if(flag)
return ;
if(k==sum)
{
flag=;
return ;
}
if(hash[i][j])
return ;
hash[i][j]=;
if(a[i]==str[k])
dfs(i+,j,k+);
if(b[j]==str[k])
dfs(i,j+,k+);
}
int main()
{
int i,j,t,Case=;
scanf("%d",&t);
while(t--)
{
scanf("%s %s %s",&a,&b,&str);
sum=strlen(str);
flag=;
memset(hash,,sizeof(hash));
dfs(,,);
if(flag)
printf("Data set %d: yes\n",Case++);
else
printf("Data set %d: no\n",Case++);
}
return ;
}

dp:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[][];
int main()
{
int i,j,l1,l2,sum,t,Case=;
char a[],b[],str[];
scanf("%d",&t);
while(t--)
{
scanf("%s%s%s",&a,&b,&str);
l1=strlen(a);
l2=strlen(b);
memset(dp,,sizeof(dp));
dp[][]=;
for(i=;i<=l1;i++)
for(j=;j<=l2;j++)
{
if(i>&&a[i-]==str[i+j-]&&dp[i-][j])
dp[i][j]=;
if(j>&&b[j-]==str[i+j-]&&dp[i][j-])
dp[i][j]=;
}
if(dp[l1][l2])
printf("Data set %d: yes\n",Case++);
else
printf("Data set %d: no\n",Case++);
}
return ;
}

hdu1501 动态规划的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. iPhone各种尺寸

    iPhone 6 Plus      736x414 points      2208x1242 pixels     3x scale     1920x1080 physical pixels   ...

  2. DelphiXE Android的所有权限按照分类总结说明

    相关资料:http://www.delphitop.com/html/Android/2778.html   网络相关的: android.permission.INTERNET 允许程序打开网络套接 ...

  3. HDU 4891 The Great Pan (模拟)

    The Great Pan 题目链接: http://acm.hust.edu.cn/vjudge/contest/123554#problem/D Description As a programm ...

  4. 递归函数recursion

    1(调用自身)递归函数是‘自己调用自己‘的函数,不管这样的调用是直接的还是间接的. 2(递归出口)因为函数不可以一直调用自己,无法停止工作,所以递归函数一定具备结束条件. http://www.cnb ...

  5. [iOS基础控件 - 6.11.2] - UINavigationController 多控制器 简单使用

    A.概念 1.通常一个app有多个控制器 2.需要对这些控制器进行管理 3.有多个view的时候,用一个父view去管理多个子view 4.控制器管理也是如此,使用给一个父控制器,去控制子控制器   ...

  6. eclipse scons 使用指南

    http://sconsolidator.com/projects/sconsolidator/wiki/Getting_Started Add SCons support to an existin ...

  7. cocos2d-x CCTableView

    转自:http://www.cnblogs.com/dcxing/archive/2013/01/16/2862068.html CCTableView在游戏中一般用在背包这样场景或层中,当然也不止这 ...

  8. PostgreSQL的 initdb 源代码分析之十一

    继续分析: /* Top level PG_VERSION is checked by bootstrapper, so make it first */ write_version_file(NUL ...

  9. cocos2dx 3.0 触摸机制

    在cocos2dx 3.0版本号中,废弃了以往2.x版本号的写法,我们先来看一下Layer.h中的一段代码 /* Callback function should not be deprecated, ...

  10. CDOJ 482 Charitable Exchange bfs

    Charitable Exchange Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/s ...