子串和

来源:互联网 发布:小米44g网络怎样设置 编辑:程序博客网 时间:2024/06/05 14:26
<div class="problem-display" style="font-size: 14px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><h2 style="margin: 0px; padding: 0px; font-size: 18px; text-align: center; color: rgb(113, 32, 21); font-family: 微软雅黑, 黑体;">子串和</h2><div class="problem-ins" style="text-align: center;">时间限制:<span class="editable highlight" id="problem[time_limit]" style="color: rgb(113, 32, 21);">5000</span> ms  |  内存限制:<span class="editable highlight" id="problem[memory_limit]" style="color: rgb(113, 32, 21);">65535</span> KB</div><div class="problem-ins" style="text-align: center;">难度:<span class="editable highlight" style="color: rgb(113, 32, 21);">3</span></div></div><div class="clr" style="clear: both; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun; font-size: 12.800000190734863px; line-height: 15.199999809265137px;"></div><dl class="problem-display" style="margin: 0px; padding: 0px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><dt style="font-size: 16px; margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-weight: bold;">描述</dt><dd style="font-size: 14px; margin: 0px; padding: 0px;"><span style="color: rgb(18, 28, 70); font-family: Verdana, 宋体;">给定一整型数列{a<sub>1</sub>,a<sub>2</sub>...,a<sub>n</sub>},找出连续非空子串{a<sub>x</sub>,a<sub>x+1</sub>,...,a<sub>y</sub>},使得该子序列的和最大,其中,1<=x<=y<=n。</span></dd><div class="clr" style="font-size: 14px; clear: both;"></div><dl class="others" style="margin: 0px; padding: 0px;"><dt style="font-size: 16px; margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-weight: bold;">输入</dt><dd style="font-size: 14px; margin: 0px; padding: 0px;">第一行是一个整数N(N<=10)表示测试数据的组数)每组测试数据的第一行是一个整数n表示序列中共有n个整数,随后的一行里有n个整数I(-100=<I<=100),表示数列中的所有元素。(0<n<=1000000)</dd><dt style="font-size: 16px; margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-weight: bold;">输出</dt><dd style="font-size: 14px; margin: 0px; padding: 0px;">对于每组测试数据输出和最大的连续子串的和。</dd><dt style="font-size: 16px; margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-weight: bold;">样例输入</dt><dd style="font-size: 14px; margin: 0px; padding: 0px;"><pre id="sample_input" style="margin-top: 0px; margin-bottom: 0px; padding: 5px 10px; font-family: Consolas, 'Courier New', 'DejaVu Sans Mono', 'Droid Sans Mono', monospace; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); min-height: 20px; line-height: 1.5em;">151 2 -1 3 -2
样例输出
5
提示
输入数据很多,推荐使用scanf进行输入
来源
经典问题
上传者
张云聪

这种类型题做过,杭电上一道,也让求起末位置,再回顾一下下……记着。。
#include<iostream>using namespace std;int main(){int N,i,num,sum,max;cin>>N;while(N--){  max=-200;  sum=0;      int n;  cin>>n;  for(i=0;i<n;i++)  {  cin>>num;  sum=sum+num;//  1 2 -1 3 -2  if(sum>max) //-5 3 3 -9 2 2 24  {              max=sum;  }  if(sum<0)  {  sum=0;  }  }  cout<<max<<endl;}return 0;}
<span style="font-size:24px;">下面start 和end 是最大字串和的起末位置。。</span>
#include<iostream>using namespace std;int main(){int N,i,num,sum,max,start,end,k;cin>>N;while(N--){  max=-200;  sum=0;  k=start=end=1;      int n;  cin>>n;  for(i=0;i<n;i++)  {  cin>>num;  sum=sum+num;//  1 2 -1 3 -2  if(sum>max) //-5 3 3 -9 2 2 24  {              max=sum;  start=k;  end=i+1;  }  if(sum<0)  {  sum=0;  k=i+2;  }  }  cout<<max<<endl;}return 0;}
0 0
原创粉丝点击