標籤:刷題 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