这题和往常括号匹配不同 这样的()()() 比配长度应该是6 ,这题要计算的是子串中满足条件串的长。
用栈保存,另开一个数组保存上次匹配的结果,每次都加上上次的结果。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define oo 0x3f3f3f3f
#define maxn 1000005
char str[maxn];
int leftBracket[maxn], top;
int num[maxn];
int main()
{
int max, cnt;
while (scanf("%s", str) != EOF)
{
top = max = 0;
cnt = 1;
memset(num, 0, sizeof num);
for (int i = 0; str[i]; i++)
{
if (str[i] == '(')
leftBracket[++top] = i;
else if (top)
{
int pos = leftBracket[top];
num[i] = i - pos + 1 + num[pos - 1];
--top;
if (num[i] > max)
{
max = num[i];
cnt = 1;
}
else if (num[i] == max)
cnt++;
}
}
printf("%d %d\n", max, cnt);
}
return 0;
}