首页 > 代码库 > Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

经过了一个多月的时间,今天终于可以回到正轨了,继续开始刷CF。


题目大意:

给出一个只有括号的字符串,求最长“匹配”子串的长度和数量。


解题思路:

设置数组记录匹配括号段的开头。


下面是代码:

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <cctype>
#include <algorithm>

#define eps 1e-10
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define zero(a) fabs(a)<eps
#define iabs(x)  ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A))))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )

using namespace std;

char s[1000006];
//char sta[1000006]
int op[10000006];
//int ed[10000006];
int main()
{
    scanf("%s",s);
    int top=0,len=strlen(s),bt=0;
    int max1=0,maxcnt=1;
    clearall(op,-1);
    for(int i=0; i<len; i++)
    {
        if(s[i]=='(')
        {
            if(op[top]==-1)
                op[top]=i;
            top++;
            // ed[top++]=i;
        }
        else
        {
            if(top==bt)
            {
                top++;
                bt++;
            }
            else
            {
                if(i-op[top-1]+1>max1)
                {
                    max1=i-op[top-1]+1;
                    maxcnt=1;
                }
                else if(i-op[top-1]+1==max1)
                {
                    maxcnt++;
                }
                op[top]=-1;
                top--;
            }
        }
    }
    printf("%d %d",max1,maxcnt);
    return 0;
}


Codeforces Beta Round #5 C. Longest Regular Bracket Sequence