码迷,mamicode.com
首页 > 其他好文 > 详细

Find longest contiguous sub array

时间:2014-09-09 21:20:49      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   ar   for   2014   art   

  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 ‘#‘.

  Think for a while......

  Typical DP-solved question.

  

  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

 

Find longest contiguous sub array

标签:style   blog   color   os   io   ar   for   2014   art   

原文地址:http://www.cnblogs.com/zhouyoulie/p/3963327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!