题目描述

给一个浮点数序列,取最大乘积连续子串的值,例如 -2.5,4,0,3,0.5,8,-1,则取出的最大乘积连续子串为3,0.5,8。

也就是说,上述数组中,3 0.5 8这3个数的乘积3*0.5*8=12是最大的,而且是连续的。

算法1:

首先,枚举的话,复杂度是 O(N^2)

算法2:O(N)

首先发现这个题与最长连续子数组的和非常类似。

考虑用 D.P. 来求解。

定义  max_vec[i] ----- 以 a[i] 结尾的子数组的最大乘积

min_vec[i] ------ 以 a[i] 结尾的子数组的最小乘积。

为什么要定义 min_vec 呢?? 稍微讲解。。

现在考虑,如何求解 max_vec[i]??

它的值有以下两种可能:

1. a[i]

2. a[i]  * 【a[i] 前面连续的若干个元素的乘积】;

1> if a[i] >= 0,为了让乘积最大,我们取 a[i] * max_vec[i-1] 即可;

2> if a[i] < 0,   为了让乘积最大,我们希望 【a[i]前面连续个若干个元素的乘积】越小越好,因此取 a[i] * min_vec[i-1]

从上面的分析,已经证明了 问题的最优子结构,所以 D.P. 可行。

代码如下:

/**
* @file max-multiphy.cc
* @brief max multiphy of a vector
* @author shoulinjun@126.com
* @version 0.1.00
* @date 2014-03-26
*/ #include <iostream>
#include <vector>
using namespace std; double MaxMultiply(double *a, int n)
{
double result(a[0]);
vector<double> max_vec(n), min_vec(n);
max_vec[0] = a[0];
min_vec[0] = a[0]; for(int i=1; i<n; ++i)
{
max_vec[i] = max(a[i], max(a[i]*min_vec[i-1], a[i]*max_vec[i-1]));
min_vec[i] = min(a[i], min(a[i]*min_vec[i-1], a[i]*max_vec[i-1]));
result = max(result, max_vec[i]);
}
return result;
}

Interview----最长连续乘积字串的更多相关文章

  1. 返回字符串中最长连续相同字串的长度---正则实现与JavaScript实现

    JavaScript 实现 let str = 'AAABBAACCAAAADDE' function continuousString(str) { let finalObj = {} let te ...

  2. 动态规划--求最大连续子数组的和(Python实现)&求解最大连续乘积字串(Python实现)

    def MaxSum(self,array,n): sum=array[0] result=array[0] for i in range(0,n): if sum<0: sum=a[i] el ...

  3. 最长公共字串(LCS)最长连续公共字串(LCCS)

    链接1:http://blog.csdn.net/x_xiaoge/article/details/7376220 链接2:http://blog.csdn.net/x_xiaoge/article/ ...

  4. leetcode-3 最长无重复字串

    3. Longest Substring Without Repeating Characters 题面 Given a string, find the length of the longest ...

  5. 最长连续回文串(最优线性时间O(n))

    转自:http://blog.csdn.net/hopeztm/article/details/7932245 Given a string S, find the longest palindrom ...

  6. 马拉车 o(n)(最长连续回文串) hdu 3068

    #include<bits/stdc++.h> ; using namespace std; +]; string manacher(string ss) { string tt=&quo ...

  7. (字符串)最长公共字串(Longest-Common-SubString,LCS)

    题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...

  8. 最长公共子序列(LCS)问题 Longest Common Subsequence 与最长公告字串 longest common substr

    问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk ...

  9. URAL 1517 Freedom of Choice(后缀数组,最长公共字串)

    题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b, ...

随机推荐

  1. [BI基础] ( 商务智能 ) 简介

    一.什么是BI BI(商务智能)通过给海量云数据制定“游戏规则”(对不同主题进行不同分析),将分散的数据进行搜集.整合.清理和诊断,借助一定的分析手段,进而将数据转化为信息和知识,快速准确的提供报表并 ...

  2. Java并发编程:阻塞队列(转载)

    Java并发编程:阻塞队列 在前面几篇文章中,我们讨论了同步容器(Hashtable.Vector),也讨论了并发容器(ConcurrentHashMap.CopyOnWriteArrayList), ...

  3. java操作数据库出现(][SQLServer 2000 Driver for JDBC]Error establishing socket.)的问题所在即解决办法

    在进行jdbc直接操作数据库时    : 我们需要对该工程进行一次导包(引入"msbase.jar" "mssqlserver.jar" "msuti ...

  4. Path Sum [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...

  5. [转]CentOS更改yum源与更新系统

    [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cent ...

  6. apache2.4域名配置参数

    apache2.4和 2.2版本的配置有区别 2.4的配置如下 <VirtualHost *:80>DocumentRoot /www/web/projectServerName www. ...

  7. IKAnalyzer 和 solr4.3 冲突

    solr4.3 运行之后发现异常:Exception in thread "main" java.lang.VerifyError: class org.wltea.analyze ...

  8. C#图书资源【更新中...】喜欢的就转存吧

    百度分享暂时取消了,需要的朋友可以直接关注我,我发给你. 重点篇 1.C#与.NET3.5高级程序设计 2.C#与.NET3.5高级程序设计.rar 3.Effective_C#_中文版_改善C#程序 ...

  9. 修改linux 文件权限命令 chmod

    [转载自:http://www.cnblogs.com/avril/archive/2010/03/23/1692809.html] Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以 ...

  10. Sharepoint2010突然之间不能打开页面,报503错误The service is unavailable

    原因:安装Sahrepoint时的账号出现故障,可能是密码过期等等. 解决方案: 新建windows用户ada,密码设置为永不过期,隶属于:administrators/IIS-WPG/WSS-WPG ...