大侠们帮看看对不对。。。
思想:从第一个开始扫描 遇到 (, i++
遇到 ), 如果i>0 {i--; 如果i==0 或者下一个字符不是),说明找到一个local
longest
match,reset i=0,比较这个是不是目前最大match, 是就存起来,不是就抛弃}
------------------------------------
void matchs(char* str, int& max, int &num){
int i =0; max=0, num=0;
int curr_len=0;
if(*str == '\0' || str == NULL) {max=0; num=0; return;}
//if there is a match, put the len of this match to vector
vector match;
while(*str != '\0'){
char next = *(str+1);
if(*str == '('){
i++;
}
else{
if(i>0) {
i--;
curr_len += 2;
//find a match
if(i==0 || next != ')'){
if(curr_len >= max) {
max = curr_len;
match.push_back(max);
}
curr_len=0;
i=0;
}
}
}
cout<str++;
}
//compute the number of matchs, which have the maximum lenth
vector::iterator it;
for(it = match.begin(); it!= match.end(); it++)
if(*it==max) num++;
}