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");elsecout<<ans1<<" "<<ans2<<endl;
}

转载于:https://www.cnblogs.com/qscqesze/p/4593694.html

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp相关推荐

  1. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 括号序列 dp+栈

    点击打开链接 题意: 给你一个括号序列,让你找到最长的连续的合法括号序列 然后让你输出这个括号序列的长度是多少 这么长的括号序列一共有多少个 思路: 看到括号匹配,就用stack来弄就好了 然后我们d ...

  2. 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)

    题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 1 #include <bits/s ...

  3. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈

    C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...

  4. 02.08 Longest Regular Bracket Sequence

    最长的常规支架序列|断续器 (jxnu.edu.cn)https://acs.jxnu.edu.cn/problem/CF5C 描述: This is yet another problem deal ...

  5. Codeforces Beta Round #8 C. Looking for Order 状压dp

    题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...

  6. Codeforces Beta Round #52 (Div. 2) D. Changing a String DP输出方案

    https://codeforces.com/contest/56/problem/D 就是编辑距离的加强版,需要输出方案,那么状态转移方程应该比较常规了,设 d p [ i ] [ j ] dp[i ...

  7. Codeforces Beta Round #95 (Div. 2) 部分解题报告 (dp,组合数,)

    做这样的比赛既考快速编码的能力,还有快速思维的能力.本人很弱,跌了rating..加油!!!.. 第一题上来就把题意理解错了..粗心啊..直接模拟着做就行:1:如果字符串全是大写字母就进行大小写转换: ...

  8. codeforces beta round 1

    codeforces beta round 1 A Theatre Square in the capital city of Berland has a rectangular shape with ...

  9. Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)

    Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...

最新文章

  1. 【Ubuntu】解决ubuntu系统root用户下Chrome无法启动问题
  2. Android ANR产生的原理和如何避免
  3. opc服务器自动更新,ZOPC Server(OPC服务器软件)
  4. 王者S19服务器维护时间表,王者荣耀s19buff刷新时间一览 红蓝BUFF刷新间隔是多久...
  5. python uiautomation_使用python UIAutomation从QQ2017(v8.9)群界面获取所有群成员详细资料,...
  6. SharePoint 2013 本地创建解决方案
  7. 上传服务器响应失败,Django CKEditor 上传图片提示“不正确的服务器响应”的解决办法...
  8. mysql查看前十行_head 命令 读取文件的前n行,默认查看文件的前十行
  9. 20.Linux 账号管理与 ACL 权限设置
  10. VS程序发布之NetFramework版本选择
  11. 商业画布是什么?有什么免费的模板和工具?
  12. hp440g5拆机_HP 惠普 ProBook 440 G5 评测 | ZMMOO
  13. MySQL和Navicat for MySQL下载及安装详细教程
  14. 系统的性能与压力测试
  15. java自己写母版_Java 创建并用应用幻灯片母版
  16. C语言谭浩强第三版第八章例题及课后题:函数
  17. PyTorch中view的用法
  18. 鸿蒙系统大文件夹怎么调整大小,win7系统打开窗口太大或太小的调整方法
  19. 机器人运动估计——IMU运动方程与ESKF原理介绍(下)
  20. 软件工程个人阅读作业——阅读和调研

热门文章

  1. 群体结构分析软件Structure使用步骤
  2. python处理日志_python日志处理(logging模块)
  3. 安卓如何调出软键盘_如何系统学习手机摄影?这张思维导图告诉你
  4. python中datetime模块常用方法_Python中datetime的使用和常用时间处理
  5. python实现实例_用python语言实现斗地主基础版-案例
  6. OPENCV标定外参
  7. [js]可折叠的框架效果
  8. 双主数据库配置与应用
  9. 前端技术周刊 2018-03-02
  10. Sqli-labs less 62