博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1501 动态规划
阅读量:4496 次
发布时间:2019-06-08

本文共 1687 字,大约阅读时间需要 5 分钟。

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

分别贴代码:

记忆化搜索:

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

dp:

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

 

转载于:https://www.cnblogs.com/wangfang20/p/3177979.html

你可能感兴趣的文章
【Linux】zabbix_server自启动脚本
查看>>
普通用户开通sudo权限:xxx is not in the sudoers file.This incident will be reported.的解决方法...
查看>>
GNU make
查看>>
Visual Studio 2008 不能更改安装目录的原因
查看>>
threejs学习笔记04---相机动
查看>>
SAP Skill - How to search a field for which table it belongs
查看>>
parcel+vue入门
查看>>
基数排序
查看>>
有意思的网站
查看>>
任务20:DI初始化的源码解读 & 任务21:依赖注入的使用
查看>>
TypeScript完全解读(26课时)_10.TypeScript完全解读-枚举
查看>>
构建之法4
查看>>
String to Integer (atoi)
查看>>
Servlet API
查看>>
【iCore4 双核心板_FPGA】例程十:FSMC总线通信实验——复用地址模式
查看>>
PyCharm命令行输入
查看>>
ThinkPHP5.0 模型更新操作
查看>>
鼠标经过时 表格行的背景色更改
查看>>
Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
查看>>
js数组操作总结
查看>>