Description
This was yet another problem dealing with regular bracket sequences.
We should remind you a bracket sequence are called regular, if by inserting«+»and«1»into It we can get a C Orrect mathematical expression. For example, Sequences«(()) ()»,«()»and«(() (())»are Regular, while«) (»,«(()»and«(())) (»a Re not.
You are given a string of«(»and«)»characters. You were to find it longest substring that was a regular bracket sequence. You is 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 6.
Output
Print the length of the longest substring is a regular bracket sequence, and the number of such substrings. If There is 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 LL; const int OO = 1e9;const double PI = ACOs ( -1); const int N = 1e6+7;char Str[n];int len[n];/**< If this position is ') ' save start from string header to this bit The maximum number of matches */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;}
Brackets Match Codeforces 5C