链接:https://vjudge.net/problem/HDU-1501

题意:

给定3个字符串,在不改变原来顺序上能否用第一个和第二个组成第三个。

思路:

dp。dp[i][j]表示可以用第一个字符串前i位和第二个字符串前j位,组成第三个字符串前i+j位。

两重循环。

考虑每一次ij,当dp[i-1][j]为1 && a[i-1] == c[i+j-1] 时, dp[i][j] = 1;j同理。

DFS。Vis[i][j]记录到达过此种状态不用考虑。

代码:

dp

#include <iostream>
#include <memory.h>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <queue>
#include <string>
#include <stack>
#include <iterator>
#include <stdlib.h>
#include <time.h>
#include <assert.h>using namespace std;
typedef long long LL;const int MAXN = 1000 + 10;
int dp[MAXN][MAXN];int main()
{int t, cnt = 0;string a, b, c;cin >> t;while (t--){cin >> a >> b >> c;memset(dp, 0, sizeof(dp));/*for (int i = 1;i <= a.length();i++)dp[i][0] = (a[i-1] == c[i-1]);for (int i = 1;i <= b.length();i++)dp[0][i] = (b[i-1] == c[i-1]);*/if (a[0] == c[0])dp[1][0] = 1;if (b[0] == c[0])dp[0][1] = 1;for (int i = 0;i <= a.length();i++){for (int j = 0;j <= b.length();j++){if (dp[i][j-1] && b[j-1]==c[i+j-1])dp[i][j] = 1;if (dp[i-1][j] && a[i-1]==c[i+j-1])dp[i][j] = 1;}}if (dp[a.length()][b.length()])printf("Data set %d: yes\n",++cnt);elseprintf("Data set %d: no\n",++cnt);}return 0;
}

DFS

#include <iostream>
#include <memory.h>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <queue>
#include <string>
#include <stack>
#include <iterator>
#include <stdlib.h>
#include <time.h>
#include <assert.h>using namespace std;
typedef long long LL;const int MAXN = 1000 + 10;
int Vis[MAXN][MAXN];
bool flag = false;
string a, b, c;void Dfs(int x, int y, int z)
{if (z == c.length()){flag = true;return ;}if (Vis[x][y])return;Vis[x][y] = 1;if (a[x] == c[z])Dfs(x+1, y, z+1);if (b[y] == c[z])Dfs(x, y+1, z+1);
}int main()
{int t, cnt = 0;cin >> t;while (t--){cin >> a >> b >> c;memset(Vis, 0, sizeof(Vis));flag = false;Dfs(0, 0, 0);if (flag)printf("Data set %d: yes\n",++cnt);elseprintf("Data set %d: no\n",++cnt);}return 0;
}

转载于:https://www.cnblogs.com/YDDDD/p/10665123.html

HDU-1501-Zipper相关推荐

  1. HDU 1501 Zipper

    记录一下第i个放入之后可能分割的情况,然后可以推出放入第i+1个的分割情况. #include<cstdio> #include<cstring> #include<cm ...

  2. 【HDU 1501】Zipper(记忆化搜索)

    传送门 我们记录pos1 pos2 pos3 分别代表现在字符串1,2,3的位置 然后判断pos1是否等于pos3 或者pos2是否等于pos3 分别进行dfs 然后我们发现是可以记忆化的 比方当po ...

  3. 杭电 1501 zipper(典型dfs)

    写在前面的: 最近一直在做搜索,这个题目也是DFS一道很典型的题目,一开始想不用搜索做,结果发现思路是错的,大神给了我几组样例之后就只能重拍了.所以开始考虑DFS.一开始没看出来时搜索,所以按自己方法 ...

  4. 杭电OJ分类题目(3)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(3) HDU Computational Ge ...

  5. HDU OJ 动态规划46题解析

    Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955  背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢 ...

  6. HDU中一些DP的题目分类

    DP是难点,供自已以后系统学习. 1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包;第一次做的时候把概率当做 ...

  7. hdu 动态规划题集

    原文链接:http://blog.sina.com.cn/s/blog_6cf509db0100sptt.html点击打开链接 1.Robberies 连接 :http://acm.hdu.edu.c ...

  8. hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)

    HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包 ...

  9. [转] HDU 题目分类

    转载来自:http://www.cppblog.com/acronix/archive/2010/09/24/127536.aspx 分类一: 基础题:1000.1001.1004.1005.1008 ...

  10. HDU题目分类啊!!!

    分类一(详细): 分类二: 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.10 ...

最新文章

  1. Linux下的一个图形管理工具webmin
  2. centos6.5 VNC安装步骤
  3. insertAdjacentHTML方法详解
  4. comparator java_java:Comparator比较器
  5. java php net_网页快速入门(asp aps.net php java)
  6. linux top命令VIRT,RES,SHR,DATA的含义
  7. (032) Linux之shell流控制for循环
  8. Android开发 - 掌握ConstraintLayout(七)辅助线(Guideline)
  9. c#中WepAPI(post/get)控制器方法创建和httpclient调用webAPI实例
  10. 整合axis2到web项目中
  11. Photoshop 2020 新建文档窗口闪烁,点击创建按钮无效的解决办法(继续使用新版)
  12. 云游戏的架构设计和技术实现
  13. 谷歌GDELT数据说明
  14. 沪深股票的复权计算(复权因子的应用)
  15. LaTeX数学公式常用符号
  16. excel 分组排名 countif
  17. 新媒体运营编辑有出路吗
  18. 网易16年研发实习生笔试题 - 寻找第K大
  19. 雅思-我们遇到过的哪些熟悉又陌生的单词1
  20. 基于MATLAB的人脸识别 (1)

热门文章

  1. 2017/05/04 java 基础 随笔
  2. L2-008. 最长对称子串
  3. Light OJ Dynamic Programming
  4. OpenGIS--GDAL库学习笔记
  5. 远程管理 Mac OSX 设备
  6. docker学习系列11 多阶段镜像构建
  7. postgresql 数据库基本操作
  8. SCCM 2012系列14 操作系统播发③
  9. Android开发中的单元测试
  10. 在Paint事件中绘制控件(边框)