Find longest contiguous sub array

  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).

  1 /*************************************************
  2 Author:Zhou You
  3 Time:2014.09.09
  4 *************************************************/
  5 #include <iostream>
  6 #include <cstdio>
  7 
  8 using namespace std;
  9 
 10 struct matrix_element
 11 {
 12 public:
 13     matrix_element():
 14         star_num_(0),
 15         hash_num_(0){}
 16 
 17     matrix_element(unsigned star_num,unsigned hash_num):
 18         star_num_(star_num),
 19         hash_num_(hash_num){
 20     }
 21 
 22     unsigned star_num_;
 23     unsigned hash_num_;
 24 };
 25 
 26 void BuildMatrix(matrix_element *** pmaze,unsigned row_num,unsigned column_num)
 27 {
 28     *pmaze = new matrix_element*[row_num];
 29     for(unsigned i=0;i<row_num;++i){
 30         (*pmaze)[i] = new matrix_element[column_num];
 31     }
 32 }
 33 
 34 void ReleaseMatrix(matrix_element ***pmaze,unsigned row_num)
 35 {
 36     if(!pmaze) return;
 37 
 38     for(unsigned i=0;i<row_num;++i){
 39         delete [](*pmaze)[i];
 40     }
 41 
 42     delete [](*pmaze);
 43 }
 44 
 45 void CoreSolve(char **parray,unsigned element_num)
 46 {
 47     matrix_element **pnote = NULL;
 48     BuildMatrix(&pnote,element_num,element_num);
 49 
 50     for(unsigned i=0;i<element_num;++i){
 51         if((*parray)[i]=='*'){
 52             ++pnote[i][i].star_num_;
 53         }else if((*parray)[i]=='#'){
 54             ++pnote[i][i].hash_num_;
 55         }
 56     }
 57 
 58     int index_start = -1,index_end = -1;
 59     unsigned cur_length = 0;
 60 
 61     for(unsigned sub_array_length = 2;sub_array_length<=element_num;++sub_array_length){
 62         for(unsigned i=0;i<=element_num-sub_array_length;++i){
 63             pnote[i][i+sub_array_length-1].hash_num_ =
 64             pnote[i][i+sub_array_length-2].hash_num_+
 65             pnote[i+sub_array_length-1][i+sub_array_length-1].hash_num_;
 66 
 67             pnote[i][i+sub_array_length-1].star_num_ =
 68             pnote[i][i+sub_array_length-2].star_num_+
 69             pnote[i+sub_array_length-1][i+sub_array_length-1].star_num_;
 70 
 71             if(pnote[i][i+sub_array_length-1].star_num_==
 72                pnote[i][i+sub_array_length-1].hash_num_){
 73                 if(sub_array_length>cur_length){
 74                     cur_length = sub_array_length;
 75                     index_start = i;
 76                     index_end = i+sub_array_length-1;
 77                 }
 78             }
 79         }
 80     }
 81 
 82     cout<<index_start<<" "<<index_end;
 83 
 84     ReleaseMatrix(&pnote,element_num);
 85 }
 86 
 87 void Solve()
 88 {
 89     unsigned element_num = 0;
 90     cin>>element_num;
 91 
 92     char *parray = new char[element_num];
 93     for(unsigned i=0;i<element_num;++i){
 94         cin>>parray[i];
 95     }
 96 
 97     CoreSolve(&parray,element_num);
 98     delete []parray;
 99 }
100 
101 int main()
102 {
103     freopen("data.in","r",stdin);
104     freopen("data.out","w",stdout);
105 
106     unsigned case_num = 0;
107     cin>>case_num;
108 
109     for(unsigned i=1;i<=case_num;++i){
110         cout<<"Case #"<<i<<" ";
111         Solve();
112         cout<<endl;
113     }
114 
115     return 0;
116 }

  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

 

posted @ 2014-09-09 20:51  zhouyoulie  阅读(246)  评论(0编辑  收藏  举报