版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年大安区教育事业单位招聘真题
- 2025年江西南昌师范学院招聘真题
- 鄂尔多斯市准格尔旗卫健系统招聘考试真题2025
- 中考满分作文范文 窗前那棵树
- 特种设备安全监察条例
- 水利工程物资管理制度
- 教育系统高质量发展工作开展情况汇报范文
- 教师矛盾化解不到位问题清单及整改措施
- 交通运输企业安全管理人员培训考试试卷(附答案)
- 机关单位防震减灾工作制度
- 2026年领导干部任前廉政法规知识竞赛试题库及答案
- 2026中国社会科学院招聘土木工程师5人(北京)笔试备考试题及答案详解
- 成都未来科技城发展服务局2026年社会招聘笔试题库附参考答案详解【模拟题】
- 烧结多孔砖生产施工方案及技术措施
- 矿泉水授权销售合同
- 山东能源定向委培考试题
- 2025广东省风力发电有限公司山西分公司招聘7人笔试历年难易错考点试卷带答案解析
- 2025~2026学年安徽合肥市第四十五中学九年级上学期期末考试化学试卷
- 惠州市广东惠州市生态环境局博罗分局和博罗县污染防治攻坚战总指挥部办公室招聘编笔试历年参考题库典型考点附带答案详解
- 骨科疾病常见诊疗常规
- 2025年四川省省级机关公开遴选考试真题(附答案)
评论
0/150
提交评论