括弧匹配 CodeForces 5C

來源:互聯網
上載者:User

標籤:

Description

This is yet another problem dealing with regular bracket sequences.

We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.

You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.

Input

The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.

Output

Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".

Sample Input

Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1
#include <cstdio>#include <queue>#include <stack>#include <cmath>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;const int oo = 1e9;const double PI = acos(-1);const int N = 1e6+7;char str[N];int len[N];/**< 如果這個位置是‘)‘ 儲存從字串頭部開始到這個位置的最大匹配數量 */int main(){    int i, ans=0, sum=0;    stack <int >sta;    scanf("%s", str);    int M = strlen(str);    for(i = 0; i < M; i++)    {        if(str[i] == ‘(‘) sta.push(i);        else if(sta.size())        {            int top = sta.top();            sta.pop();            if(str[top-1] == ‘)‘)                len[i] = (i-top+1) + len[top-1];            else                len[i] = (i-top+1);            ans = max(ans, len[i]);        }    }    for(i = M-1; i >= 0; i--)    {        if(str[i] == ‘)‘ && len[i] && len[i] == ans)        sum++;    }    if(sum == 0) sum = 1;    printf("%d %d\n", ans, sum);    return 0;}

  

括弧匹配 CodeForces 5C

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.