The longest valid segment of the 51nod 1478 bracket sequence

Source: Internet
Author: User

Here's another question about handling the legal parentheses sequence.

If you insert "+" and "1" to a sequence of parentheses, we get a correct mathematical expression, and we think that this bracket sequence is legitimate. For example, the sequence "(()) ()", "()" and "(() ())" is legal, but "(", "()" and "(())) (" is not lawful.)

There is a string that contains only "(" and ")", you need to find the longest valid bracket segment, and you have to find the number of substrings with the longest length.
Input

The first line is a non-empty string that contains only "(" and ")". It has a length of not more than 1000000.
Output
The length of the oldest string and the number of the oldest string in the sequence of qualified parentheses. If you do not have such a substring, you only need to output a row of "0 1".
Input sample
)((())))(()())
Output sample

6 2

Idea: A piece of string is the same as the number of (and), and the opening is (end is). We can force One) and the nearest one (match, match the tag does not match, we can put (the position in a stack, so that can and the most recent match.) At the end of the statistic, as long as there is a continuous mark, that paragraph is legal, directly matching the longest.

 #include <bits/stdc++.h> using namespace std; typedef long long ll; int INF = 0x3f3f3f3f; const INT
N = 1e6 + 10;
Char S[n];
int vis[n];
    int main () {scanf ("%s", s+1);
    memset (Vis, 0, sizeof (VIS));
    stack<int>sta;
    int L = strlen (s+1), Maxx, tmp, a = 0;
        for (int i = 1; i<=l; i++) {if (s[i) = = ' (') sta.push (i);
                else {if (!sta.empty ()) {vis[i] = 1;
                Vis[sta.top ()] = 1;
            Sta.pop ();
    }} tmp = Maxx = 0;
       for (int i = 1; i<=l; i++) {if (vis[i)) {tmp++;
       else {tmp = 0;
       } if (tmp = = Maxx) a++;
    else if (tmp > Maxx) A = 1, Maxx = tmp;
    } if (Maxx = 0) cout<<0<< "" <<1<<endl;
    else cout<<maxx<< "" <<a<<endl;
return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.