版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、算法设计与分析作业作业一动态规划作业姓名指导教师学号日 期班级作业内容1)预习教材并查找资料,学习动态规划基本知识。2)完成并实现教材例题:最长公共子序列3)完成并实现教材例题:01背包作业解答作业2-1:求最长公共子序列A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = another sequence Z = is a subsequence of X if there exists a strict
2、ly increasing sequence of indices of X such that for all j = 1,2,.,k, x 口 = zj. For example, Z = is a subsequence of X = with index sequence . Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.InputThe program input is from the std inpu
3、t. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.OutputFor each set of data the program prints on the standard output the length of the maximum-length common subsequence from the
4、 beginning of a separate line.代码:#includecstdio#includecstring#includealgorithmusing namespace std;#define inf 1200#define loop(x,y,z) for(x=y;xz;x+)#define INF 99999999#define ll long long,j;char s1inf,s2inf;int ansinfinf;void init()memset(ans,0,sizeof 0);int getAns(int i,int j)if(i0|j0)return 0;re
5、turn ansij;int calDP()int len1=strlen(s1);int len2=strlen(s2);int i,j;loop(i,0,len1)loop(j,0,len2)if(s1i=s2j)ansij=getAns(i-1,j-1)+1;elseansij=max(getAns(i-1,j),getAns(i,j-1);return anslen1Tlen2T;int main()int i,j;while(scanf(%s%s”,s1,s2)printf(%dn”,calDP();return 0;测试数据:Sample Inputabcfbcabfcabprog
6、ramming contestabcdmnpSample Output420测试截图: E cod e66&lbinC ebu g1 .exeabcfbc 4 programning 2 abed Qabfcab contest mnp作业2-201背包问题:Many years ago , in Teddys hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dogs , cows , also he went to the
7、grave The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bones value along his trip , can you calculate out the maximum of the total value the bone co
8、llector can get ?InputThe first line contain a integer T , the number of cases. Followed by T cases , each case three lines , the first line contain two integer N , V, (N = 1000 , V = 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representin
9、g the value of each bone. The third line contain N integers representing the volume of each bone.OutputOne integer per line representing the maximum of the total value (this number will be less than 2 3).Sample Input15 101 2 3 4 55 4 3 2 1Sample Output14代码:#includecstdio#includecstring#includealgori
10、thmusing namespace std;#define inf 1200#define loop(x,y,z) for(x=y;xz;x+)#define INF 99999999#define ll long longint n,bag;int valueinf;int volumeinf;int ansinfinf;/存放结果void init()memset(ans,0,sizeof ans);void calDP()int i,j;loop(i,0,bag+1)loop(j,1,n+1)if(i-volumej0)ansij=ansij-1;elseansij=max(ansi-volumejj-1+valuej,ansij-1)int main()int T;int i,j;scanf(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 水泥发泡鱼塘施工方案(3篇)
- 青岛营销方案系统(3篇)
- 化工夏季防洪应急预案(3篇)
- 老乡鸡营销方案(3篇)
- 维修技术标准化
- 深入解析GIPR下游信号调控网络:鉴定、机制与医学启示
- 淡水养殖生物有机肥的研制与应用:技术、效果与展望
- 淀山湖浮游生物群落结构:特征解析与环境关联研究
- 液晶弹性体及结构中应力波传播特性的多维度探究
- 湖南省岳阳市云溪区2026年七年级下学期期中考试数学试题附答案
- 2026福州鼓楼攀登信息科技有限公司招聘1人笔试历年参考题库附带答案详解
- 河南省活性炭码上换监管预警系统-20260415
- 2026年山东春考《艺术设计类专业知识》模拟试题及答案解析
- 2026年事业单位公开招聘考试(综合类)试题与答案
- 工地二十四小时工作制度
- 2025年四川省省级机关公开遴选考试真题(附答案)
- 2026年统编版二年级道德与法治下册每课教学设计
- 2025年西藏拉萨市检察院书记员考试题(附答案)
- TSG08-2026《特种设备使用管理规则》全面解读课件
- 2026年疫情期间应急处置演练方案及演练记录范本
- 第三单元 认识国家制度 单元检测(含答案)-2025-2026学年八年级下册统编版道德与法治
评论
0/150
提交评论