Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

來源:互聯網
上載者:User

標籤:刷題   codeforces   水   

經過了一個多月的時間,今天終於可以回到正軌了,繼續開始刷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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.