CF 5C Longest Regular Bracket Sequence

把所有規則括號都記錄起來即可!然後統計連續的規則串,用map來記錄!用棧模擬!

不難

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <limits.h>
#include <stack>
#include <map>
using namespace std;
stack<int> s;
int cot[1000000];
map <int,int> m;
int main()
{
    string str;
    int i,j,len;
    int top;
    while(cin>>str)
    {
        len = str.size();
        memset(cot,0,sizeof(cot));
        top = 0;
        for(i = 0;i < len;i ++)
        {
            if(str[i] == '(')
            {
                s.push(i);
            }
            else
            {
                if(s.empty()) continue;
                top += 2;
                cot[s.top()] = cot[i] = 1 ;
                s.pop();
            }
        }
        //for(i = 0;i < len;i ++) cout<<cot[i];cout<<endl;
        top = 0;
        for(i = 0;i < len;i ++)
        {
            if(cot[i] == 0) {m[top]++;top = 0;}
            else top ++;
        }
        if(top!=0)
        m[top] ++;
        int anslen = 0,anssum = 1;
        map<int,int>::iterator   it;
        for(it = m.begin();it != m.end();it ++)
        {
            if(it->first > anslen) {anslen = it->first;anssum = it->second;}
        }
        cout<<anslen<<" "<<anssum<<endl;
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章