寻找最大连续子序列/Find the max contiguous subsequence
寻找最大连续子序列
给定一个实数序列X1,X2,...Xn(不需要是正数),寻找一个(连续的)子序列Xi,Xi+1,...Xj,使得其数值之和在所有的连续子序列数值之和中为最大。
一般称这个子序列为最大子序列,例如,在序列(2,-3,1.5,-1,3,-2,-3,3)中,最大的子序列是(1.5,-1,3)它的和是3.5,在一个给定的序列中可能有几个最大子序列。
如果所有的数值为负数,则最大子序列为空(由定义,空的子序列之和为0)。我们希望有一个解决该问题的算法,并且仅对此序列扫描一次。
扩展问题,算法至少还求出一个最大子序列,并且输出。
算法要点:当临时子序列之和小于零时,需要重置临时子序列之和为零,相当于重新起点计算子序列。
如下方法定义于工具类:MaxSubsequenceUtil
public static double FindMaxSubsequence(double[] array, out int startIndex)
{
double GlobeMax = , tmpMax = ;
startIndex = -;
for (int i = array.Length - ; i >= ; i--)
{
tmpMax = tmpMax + array[i];
if (GlobeMax < tmpMax)
{
GlobeMax = tmpMax;
startIndex = i;
}
else if (tmpMax < )
{
tmpMax = ;
}
}
return GlobeMax;
}
方法调用和输出子序列方法:
static void Main(string[] args)
{
double[] dArray = new double[] { , -, 1.5, -, , -, -, };
dArray.ToList<double>().ForEach(d => Console.Write("{0} ", d));
int startIndex;
double max = MaxSubsequenceUtil.FindMaxSubsequence(dArray, out startIndex);
if (max == )
{
Console.WriteLine("No max subsequence exist!");
}
else
{
Console.WriteLine();
Console.WriteLine("The max total is:{0}", max);
Console.WriteLine("The max subsequence is:");
double t = ;
for (int i = startIndex; i < dArray.Length; i++)
{
t += dArray[i];
if (t == max)
{
Console.Write("{0} ", dArray[i]);
break;
}
Console.Write("{0} ", dArray[i]);
}
}
Console.ReadKey();
}
完毕。
下载源码。
作者:Andy Zeng
欢迎任何形式的转载,但请务必注明出处。
http://www.cnblogs.com/andyzeng/p/3672547.html
寻找最大连续子序列/Find the max contiguous subsequence的更多相关文章
- hdu 1231 最大连续子序列 ,1003 Max Sum;
题目(1231) #include<stdio.h> #include<iostream> using namespace std; int main() { int K,nu ...
- K:求取数组中最大连续子序列和的四个算法
相关介绍: 求取数组中最大连续子序列和问题,是一个较为"古老"的一个问题.该问题的描述为,给定一个整型数组(当然浮点型也是可以的啦),求取其下标连续的子序列,且其和为该数组的所有 ...
- HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hdu1003 Max Sum【最大连续子序列之和】
题目链接:https://vjudge.net/problem/HDU-1003 题目大意:给出一段序列,求出最大连续子序列之和,以及给出这段子序列的起点和终点. 解题思路:最长连续子序列之和问题其实 ...
- ACM-DP之最大连续子序列——hdu1231
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] E 三分+连续子序列的和的绝对值的最大值
E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- ZOJ 3872 Beauty of Array 连续子序列求和
Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...
- 动态规划(Dynamic Programming, DP)---- 最大连续子序列和
动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想,简单来使,动态规划是将一个复杂的问题分解成若干个子问题,或者说若干个阶段,下一个阶段通过上一个阶段的结 ...
随机推荐
- 深入理解 Vuejs 动画效果
本文主要归纳在 Vuejs 学习过程中对于 Vuejs 动画效果的各个相关要点.由于本人水平有限,如文中出现错误请多多包涵并指正,感谢.如果需要看更清晰的代码高亮,请跳转至我的个人站点的 深入理解 V ...
- Paper Reading - Learning to Evaluate Image Captioning ( CVPR 2018 ) ★
Link of the Paper: https://arxiv.org/abs/1806.06422 Innovations: The authors propose a novel learnin ...
- 网络安全部门的漏洞扫描让你头痛不已么——PHP环境选它就可以了
最近网络安全要求是越来越严,原来PHP编写的程序在XAMPP或者其他环境下总会被某款软件扫出漏洞,进而上级部门就停止了我们服务器的外网出口,然而自从发现了一款安全环境神器UPUPW后,这样的问题就再也 ...
- [git] Git in Practice
Work flow with git and github Work with Remotes Check the current status git status Check the latest ...
- Fafa and the Gates(模拟)
Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens ...
- centos7编译安装redis遇坑
编译redis时:make cc Command not found 原因分析:没有安装gcc,执行: yum install gcc 编译redis时:error: jemalloc/jemallo ...
- [OS] 多线程--第一次亲密接触CreateThread与_beginthreadex本质区别
转自:http://blog.csdn.net/morewindows/article/details/7421759 本文将带领你与多线程作第一次亲密接触,并深入分析CreateThread与_be ...
- 【Python】Python time mktime()方法
描述 Python time mktime() 函数执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数. 如果输入的值不 ...
- 【bzoj2653】middle 可持久化线段树区间合并
题目描述 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个长度为n的序列s.回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[ ...
- openstack之Glance介绍
什么是Glance glance即image service(镜像服务),是为虚拟机的创建提供镜像服务 为什么要有Glance 我们基于openstack是构建基本的Iaas平台对外提供虚机,而虚机在 ...