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. DatePicker (JQ-UI) 的z-index问题

    使用jq-ui的 datepicker ,发现日期弹出层被 <div class="fixed" style="z-index: 2; position: abso ...

  2. jquery 验证插件 validate

    1)required:true 必输字段(2)remote:"check.php" 使用ajax方法调用check.php验证输入值(3)email:true 必须输入正确格式的电 ...

  3. sbrk and coreleft

    一.sbrk 函数来源:TC2.0.Linux 函数名: sbrk 功 能: 增加程序可用数据段空间,增加大小由参数 incr决定 . 返回值:函数调用成功返回一指针,指向新的内存空间.函数调用失败则 ...

  4. 'InitializeCulture' is not a member of 'XXXX'

    今天在部署ASP.NET网页时出现一个奇怪的问题.在开发,调试时访问网页一切正常.但一部署到IIS中时就会出现以下的错误信息:'InitializeCulture' is not a member o ...

  5. Windows下虚拟Linux

    andlinux cygwin virtualbox VMware XenServer

  6. percona-xtrabackup安装及使用教程

    安装percona-xtrabackup依赖包(前提已安装mysql) yum -y install perl-CPAN perl-DBD-MySQL perl make gcc gcc-c++ pa ...

  7. 关于安装完Node.js 出现node is not dedined 问题

    今天想来接触下Node.js 神奇  怀着揣揣的心 从官网下载了windows 的64位mis版本  几个下一步就顺利安装完毕了 接着跟着新手教程走  不懂教程里的 苹果shell 是什么 反正就是检 ...

  8. C语言考试解答十题

    学院比较奇葩,大一下期让学的VB,这学期就要学C++了,然后在开学的前三个周没有课,就由老师讲三个周的C语言,每天9:30~11:30听课,除去放假和双休日,实际听课时间一共是12天*2小时,下午是1 ...

  9. Apache CXF 例子

    来自:http://www.cnblogs.com/frankliiu-java/articles/1641949.html Apache CXF 是一个开放源代码框架,是在Xfire 跟Celtix ...

  10. @Component("userService").@Resource(name="userDao")

    @Component("userService") public class UserService { private UserDAO userDao; public void ...