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 is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.

You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.

Input

The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.

Output

Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".

Sample Input

)((())))(()())

Sample Output

6 2

HINT

题意

给你一个括号序列,让你找到最长的连续的合法括号序列

然后让你输出这个括号序列的长度是多少

这么长的括号序列一共有多少个

题解:

看到括号匹配,就用stack来弄就好了

然后我们再简单dp一下,表示以这个字符结尾的序列的长度是多少

然后跑一发就好了

代码

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

string s;
stack<int> k;
int dp[maxn];
int main()
{
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]==‘(‘)
            k.push(i);
        else
        {
            if(!k.empty())
            {
                dp[i]=i-k.top()+1;
                if(k.top()>0)
                    dp[i]+=dp[k.top()-1];
                k.pop();
            }
        }
    }
    int ans1=0,ans2=1;
    for(int i=0;i<s.size();i++)
    {
        if(dp[i]>ans1)
        {
            ans1=dp[i];
            ans2=1;
        }
        else if(dp[i]==ans1)
            ans2++;
    }
    if(ans1==0)
        printf("0 1\n");
    else
        cout<<ans1<<" "<<ans2<<endl;
}
时间: 2024-10-11 05:31:52

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

贪心+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

经过了一个多月的时间,今天终于可以回到正轨了,继续开始刷CF. 题目大意: 给出一个只有括号的字符串,求最长"匹配"子串的长度和数量. 解题思路: 设置数组记录匹配括号段的开头. 下面是代码: #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include &l

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