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;
}
时间: 2024-07-31 13:10:09

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence的相关文章

贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

题目传送门 1 /* 2 题意:求最长括号匹配的长度和它的个数 3 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 4 详细解释:http://blog.csdn.net/taoxin52/article/details/26012167 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/5/C Description This is yet another problem dealing with regular bracket sequences. We should remind you that a bracket sequence

Codeforces Beta Round #3 D. Least Cost Bracket Sequence

看来最不擅长的就是贪心,这种方法都想不起来是不是专题刷多了?   也没见得专题做得有多好啊~ 题目大意: 给出一个字符串,包括三种字符'('.')'.'?',每个问号可以变成其他两种符号,但是需要费用. 要求组成一个符合条件的字符串,使括号匹配,求最小费用. 解题思路: 贪心(发现他比动态规划都难). 不需要在意哪个括号和哪个括号匹配,只需要注意数量就行. 下面是代码: #include <set> #include <map> #include <queue> #in

CodeForces 5C Longest Regular Bracket Sequence

题意:给定一串括号,求最长的规则('(())'或‘(()())’)的字串及最长字串的个数: 思路:使用栈保存左括号,与最近的右括号匹配,使用递推推出每个位置最长字串长度: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<stack> using namespace std; stack<int> t; int n,m,len

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

CodeForces Beta Round #1

Codeforces Beta Round #1 A. Theatre Square [题意]一个n*m的矩形广场,用a*a的方形石板铺设,问最少需要多少块石板能铺满广场. [思路]水题,从n方向来看能能够铺设ceil(n/a)块,从m方向来看能能够铺设ceil(m/a)块,总共有ceil(n/a)*ceil(m/a)块. 1 /* 2 ** CodeForces 1A Theatre Square 3 ** Created by Rayn @@ 2014/05/18 4 */ 5 #inclu

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include