欢迎来到天天文库
浏览记录
ID:20361991
大小:292.00 KB
页数:18页
时间:2018-10-10
《dp类型各题总结-acm》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、最大连续子序列TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):11540AcceptedSubmission(s):4856还需要输出该子序列的第一个和最后一个元素。对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元素,中间用空格分隔。如果最大连续子序列不唯一,则输山序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。思路:一路加如
2、果sum变成小于0了从当前位置重新新开始加(小于0的话就对整个段没有贡献了而II会使整个段的和变小)#includeintmain(){intn,i,a[10005],start,end,sum,max,sta,flag,cnt;while(l){cnt=0;sum=0;max=-1;flag=l;scanf("%d",&n);if(n==0)return0;for(i=0;i3、[O];if((cnt==n4、5、cnt==n-1)){if(flag==0){printf(’’O00H);continue;}elseif(n!=l){printf("0%d%d",a[0],a[n-l]);continue;}}for(i=0;imax)max=sum;end=a[i];start=sta;}}}printf(',o/od%d%d",max,start,end);}}Hdu1087s6、uperjumping题意:此题就是找序列屮最大升序子序列的和。可跳不一定非要连续比如132屮升序序列有:{1}{3}{2}{1,3}{1,2}。所以最大为1+3=4;#include_int64a[1005],sum[1005];intmain(){intij,n,max,ans;while(scanf(’’%d",&n)&&n){for(i=0;i7、f(a[j](sum8、j]+a[i])?sum[i]:(sum9、j]+a[i]);//suin10、j]之前已经求出來了}ans=ans〉sumfil?ans:sumfil;}printf("%d",ans);}return0;}CommonSubsequenceTimeLimit:2000/1000ms(Java/Other)MemoryLimit:65536/32768K(Java/Other)TotalSubmission(s):29AcceptedSubmission(s):16ProblemDe11、scriptionAsubsequenceofagivensequenceisthegivensequencewithsomeelements(possiblenone)leftout.GivenasequenceX=anothersequenceZ=isasubsequenceofXifthereexistsastrictlyincreasingsequenceofindicesofXsuchthatforallj=1,2,".,k,xij=zj.Forexample12、,Z=withindexsequence<1,2,4,6>.GiventwosequencesXandYtheproblemistofindthelengthofthemaximum-lengthcommonsubsequenceofXandY.Theprograminputisfromatextfile.Eachdatasetinthefilecontainstwostringsrepresentingthegivensequences.Thesequencesa13、reseparatedbyanynumberofwhitespaces.Theinputdataarecorrect.Foreachsetofdata
3、[O];if((cnt==n
4、
5、cnt==n-1)){if(flag==0){printf(’’O00H);continue;}elseif(n!=l){printf("0%d%d",a[0],a[n-l]);continue;}}for(i=0;imax)max=sum;end=a[i];start=sta;}}}printf(',o/od%d%d",max,start,end);}}Hdu1087s
6、uperjumping题意:此题就是找序列屮最大升序子序列的和。可跳不一定非要连续比如132屮升序序列有:{1}{3}{2}{1,3}{1,2}。所以最大为1+3=4;#include_int64a[1005],sum[1005];intmain(){intij,n,max,ans;while(scanf(’’%d",&n)&&n){for(i=0;i7、f(a[j](sum8、j]+a[i])?sum[i]:(sum9、j]+a[i]);//suin10、j]之前已经求出來了}ans=ans〉sumfil?ans:sumfil;}printf("%d",ans);}return0;}CommonSubsequenceTimeLimit:2000/1000ms(Java/Other)MemoryLimit:65536/32768K(Java/Other)TotalSubmission(s):29AcceptedSubmission(s):16ProblemDe11、scriptionAsubsequenceofagivensequenceisthegivensequencewithsomeelements(possiblenone)leftout.GivenasequenceX=anothersequenceZ=isasubsequenceofXifthereexistsastrictlyincreasingsequenceofindicesofXsuchthatforallj=1,2,".,k,xij=zj.Forexample12、,Z=withindexsequence<1,2,4,6>.GiventwosequencesXandYtheproblemistofindthelengthofthemaximum-lengthcommonsubsequenceofXandY.Theprograminputisfromatextfile.Eachdatasetinthefilecontainstwostringsrepresentingthegivensequences.Thesequencesa13、reseparatedbyanynumberofwhitespaces.Theinputdataarecorrect.Foreachsetofdata
7、f(a[j](sum
8、j]+a[i])?sum[i]:(sum
9、j]+a[i]);//suin
10、j]之前已经求出來了}ans=ans〉sumfil?ans:sumfil;}printf("%d",ans);}return0;}CommonSubsequenceTimeLimit:2000/1000ms(Java/Other)MemoryLimit:65536/32768K(Java/Other)TotalSubmission(s):29AcceptedSubmission(s):16ProblemDe
11、scriptionAsubsequenceofagivensequenceisthegivensequencewithsomeelements(possiblenone)leftout.GivenasequenceX=anothersequenceZ=isasubsequenceofXifthereexistsastrictlyincreasingsequenceofindicesofXsuchthatforallj=1,2,".,k,xij=zj.Forexample
12、,Z=withindexsequence<1,2,4,6>.GiventwosequencesXandYtheproblemistofindthelengthofthemaximum-lengthcommonsubsequenceofXandY.Theprograminputisfromatextfile.Eachdatasetinthefilecontainstwostringsrepresentingthegivensequences.Thesequencesa
13、reseparatedbyanynumberofwhitespaces.Theinputdataarecorrect.Foreachsetofdata
此文档下载收益归作者所有