It's still an Amazon interview question.

  Given an array containing only stars '*' and hashes '#' . Find longest contiguous sub array that will contain equal no. of stars '*' and hashes '#'.Output the index range if the longest contiguous sub array does exist or else output -1 and -1,which denote no corresponding contiguous sub array exist.

  Think for a while......

  Typical DP-solved question.Every time I scan the sub array,I can use something that has been done.Say when I want to scan [2,6] in the original array,I should know the information about [2,5] in advance.So we can scan the array by length.The code is below.Time:O(n3),Space:O(n2),(n denotes the length of the array).

 /*************************************************
Author:Zhou You
Time:2014.09.09
*************************************************/
#include <iostream>
#include <cstdio> using namespace std; struct matrix_element
{
public:
matrix_element():
star_num_(),
hash_num_(){} matrix_element(unsigned star_num,unsigned hash_num):
star_num_(star_num),
hash_num_(hash_num){
} unsigned star_num_;
unsigned hash_num_;
}; void BuildMatrix(matrix_element *** pmaze,unsigned row_num,unsigned column_num)
{
*pmaze = new matrix_element*[row_num];
for(unsigned i=;i<row_num;++i){
(*pmaze)[i] = new matrix_element[column_num];
}
} void ReleaseMatrix(matrix_element ***pmaze,unsigned row_num)
{
if(!pmaze) return; for(unsigned i=;i<row_num;++i){
delete [](*pmaze)[i];
} delete [](*pmaze);
} void CoreSolve(char **parray,unsigned element_num)
{
matrix_element **pnote = NULL;
BuildMatrix(&pnote,element_num,element_num); for(unsigned i=;i<element_num;++i){
if((*parray)[i]=='*'){
++pnote[i][i].star_num_;
}else if((*parray)[i]=='#'){
++pnote[i][i].hash_num_;
}
} int index_start = -,index_end = -;
unsigned cur_length = ; for(unsigned sub_array_length = ;sub_array_length<=element_num;++sub_array_length){
for(unsigned i=;i<=element_num-sub_array_length;++i){
pnote[i][i+sub_array_length-].hash_num_ =
pnote[i][i+sub_array_length-].hash_num_+
pnote[i+sub_array_length-][i+sub_array_length-].hash_num_; pnote[i][i+sub_array_length-].star_num_ =
pnote[i][i+sub_array_length-].star_num_+
pnote[i+sub_array_length-][i+sub_array_length-].star_num_; if(pnote[i][i+sub_array_length-].star_num_==
pnote[i][i+sub_array_length-].hash_num_){
if(sub_array_length>cur_length){
cur_length = sub_array_length;
index_start = i;
index_end = i+sub_array_length-;
}
}
}
} cout<<index_start<<" "<<index_end; ReleaseMatrix(&pnote,element_num);
} void Solve()
{
unsigned element_num = ;
cin>>element_num; char *parray = new char[element_num];
for(unsigned i=;i<element_num;++i){
cin>>parray[i];
} CoreSolve(&parray,element_num);
delete []parray;
} int main()
{
freopen("data.in","r",stdin);
freopen("data.out","w",stdout); unsigned case_num = ;
cin>>case_num; for(unsigned i=;i<=case_num;++i){
cout<<"Case #"<<i<<" ";
Solve();
cout<<endl;
} return ;
}

  Case in data.in file

5
4
*#*#
4
*#**
5
*****
6
*###**
12
****###*****

  Output data in data.out file

Case #1 0 3
Case #2 0 1
Case #3 -1 -1
Case #4 0 5
Case #5 1 6

Find longest contiguous sub array的更多相关文章

  1. [LeetCode] Longest Mountain in Array 数组中最长的山

    Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...

  2. [Swift]LeetCode845. 数组中的最长山脉 | Longest Mountain in Array

    Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...

  3. LeetCode 845. Longest Mountain in Array

    原题链接在这里:https://leetcode.com/problems/longest-mountain-in-array/ 题目: Let's call any (contiguous) sub ...

  4. 【LeetCode】845. Longest Mountain in Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...

  5. 【leetcode】845. Longest Mountain in Array

    题目如下: 解题思路:本题的关键是找出从升序到降序的转折点.开到升序和降序,有没有联想的常见的一个动态规划的经典案例--求最长递增子序列.对于数组中每一个元素的mountain length就是左边升 ...

  6. Longest Mountain in Array 数组中的最长山脉

    我们把数组 A 中符合下列属性的任意连续子数组 B 称为 “山脉”: B.length >= 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] ...

  7. [LeetCode] Contiguous Array 邻近数组

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  8. [Swift]LeetCode525. 连续数组 | Contiguous Array

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  9. 994.Contiguous Array 邻近数组

    描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...

随机推荐

  1. vim file save as

    the command of vim to save as the file :w new_file_name

  2. 树莓派2 安装mono3.0运行mvc4

    sudo apt-get updatesudo apt-get upgradesudo apt-get mono-completewget -c http://www.linuxdot.net/dow ...

  3. <一> MVC - HtmlHelper

    HtmlHelper类位于System.Web.Mvc.Html之中主要有七个静态类组成: FormExtensions - BeginForm, BeginRouteForm, EndForm In ...

  4. 两个表,一个表中的两列关联另一个表的id,如何将这个表中的两列显示为另一个表id对应的内容

    表A name user owner machine1 1 2 machine2 3 4 表B userid username 1 aaa 2 bbb 3 ccc 4 ddd 以上两个表,表A 设备的 ...

  5. Java程序发展之路

  6. PCB使用技巧

    1.元器件标号自动产生或已有的元器件标号取消重来Tools 工具|Annotate…注释All Part:为所有元器件产生标号Reset Designators:撤除所有元器件标号2.单面板设置:De ...

  7. Android Service with Delphi 10 Seattle

    http://delphi.org/2015/09/minimalistic-android-service-with-delphi-10-seattle/ http://delphi.org/201 ...

  8. 对加密方式(公钥私钥)的形象理解(以http和https为例)

    https其实就是建构在SSL/TLS之上的 http协议,所以要比较https比http多用多少服务器资源,主要看SSL/TLS本身消耗多少服务器资源. http使用TCP 三次握手建立连接,客户端 ...

  9. Android开发:Translucent System Bar 的最佳实践

    Translucent System Bar 的最佳实践 近几天准备抽空总结Android一些系统UI的实践使用,于是开始动手建了一个库AndroidSystemUiTraining ,边撸代码边写总 ...

  10. EqualsBuilder和HashCodeBuilder

    package com.osc.demo; import java.util.List; import org.apache.commons.lang.builder.EqualsBuilder; i ...