贪心+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 <cmath>
10 #include <stack>
11 using namespace std;
12
13 const int MAXN = 1e6 + 10;
14 const int INF = 0x3f3f3f3f;
15 char s[MAXN];
16 int len[MAXN];
17 stack<int> S;
18
19 int main(void)        //Codeforces Beta Round #5 C. Longest Regular Bracket Sequence
20 {
21 //    freopen ("C.in", "r", stdin);
22
23     while (scanf ("%s", s) == 1)
24     {
25         while (!S.empty ())    S.pop ();
26         memset (len, 0, sizeof (len));
27
28         int mx = 0;    int cnt = 0;
29         for (int i=0; s[i]; ++i)
30         {
31             if (s[i] == ‘(‘)    S.push (i);
32             else
33             {
34                 if (!S.empty ())
35                 {
36                     int p = S.top ();    S.pop ();
37                     len[i] = (p > 0 ? len[p-1] : 0) + (i - p + 1);
38                 }
39             }
40             if (len[i] > mx)    {mx = len[i];    cnt = 1;}
41             else if (len[i] == mx)    {cnt++;}
42         }
43
44         if (mx == 0)    puts ("0 1");
45         else    printf ("%d %d\n", mx, cnt);
46     }
47
48     return 0;
49 }
时间: 2024-08-29 05:10:28

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

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 #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 #3

Codeforces Beta Round #3 http://codeforces.com/contest/3 A 找规律题.但我懒得找,直接暴搜 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*

Codeforces Beta Round #5

Codeforces Beta Round #5 http://codeforces.com/contest/5 A 模拟题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLI

Codeforces Beta Round #22 (Div. 2 Only)

Codeforces Beta Round #22 (Div. 2 Only) http://codeforces.com/contest/22 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005 7 t

Codeforces Beta Round #35 (Div. 2)

Codeforces Beta Round #35 (Div. 2) http://codeforces.com/contest/35 A 这场的输入输出是到文件中的,不是标准的输入输出...没注意看,RE了好久... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x

Codeforces Beta Round 84 (Div. 2 Only)

layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 不得不说这远古场太简单了 A - Nearly Lucky Number (签到) 题意 给出一个数字 求其中7和4的数目是否是7和4组成 #include<bits/stdc++.h> using namespa