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; }