CodeForces 5C Longest Regular Backet sequence
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".
Examples
)((())))(()())
6 2
))(
0 1
题解:DP。用栈存每一个‘(’的下标,遇到一个')',判断一下栈是否为空,若为空,则继续往后,若不为空,则新增加长度为:i-temp+1;
参考代码为:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
char s[maxn];
int dp[maxn];
int main()
{
scanf("%s",s+1); stack<int> st;
int len=strlen(s+1),Max=1,ans=0;
for(int i=1;i<=len;i++)
{
if(s[i]=='(') st.push(i);
else
{
if(!st.empty())
{
int temp=st.top(); st.pop();
dp[i]=dp[temp-1]+i-temp+1;
Max=max(dp[i],Max);
}
}
}
if(Max==1) printf("0 1\n");
else
{
for(int i=1;i<=len;i++) if(dp[i]==Max) ans++;
printf("%d %d\n",Max,ans);
}
return 0;
}
CodeForces 5C Longest Regular Backet sequence的更多相关文章
- Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)
题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- 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.c ...
- 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence
题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...
- 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)
题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...
- codeforces 5C
C. Longest Regular Bracket Sequence time limit per test 2 seconds memory limit per test 256 megabyte ...
- 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 ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
- CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈
C. Replace To Make Regular Bracket Sequence time limit per test 1 second memory limit per test 256 m ...
随机推荐
- 【algo&ds】【吐血整理】4.树和二叉树、完全二叉树、满二叉树、二叉查找树、平衡二叉树、堆、哈夫曼树、B树、字典树、红黑树、跳表、散列表
本博客内容耗时4天整理,如果需要转载,请注明出处,谢谢. 1.树 1.1树的定义 在计算机科学中,树(英语:tree)是一种抽象数据类型(ADT)或是实作这种抽象数据类型的数据结构,用来模拟具有树状结 ...
- JavaScript 运行原理
i{margin-right:4px;margin-top:-0.2em}.like_comment_tips .weui-icon-success{background:transparent ur ...
- 019.Kubernetes二进制部署插件dashboard
一 修改配置文件 1.1 下载解压 [root@k8smaster01 ~]# cd /opt/k8s/work/kubernetes/ [root@k8smaster01 kubernetes]# ...
- VS安装
1. 只更改工作负载和单个组件 工作负载:我只勾选3个需要的 单个组件: 勾选 .NET 下Framework 别的不用改 2.点击安装,安装完成重启
- 开始逆向objc基础准备(二)我的平台是simulator-x86_64
建项目运行中断调试,lldb中显示寄存器看到有rax-r15, stm0-stm7, xmm0-xmm15, ymm0-ymm15,即为x64体系支持sse4. 再在lldb中查看寄存器别名得到以下对 ...
- 在lldb调试中调用c++函数
在lldb调试时,调用oc对象的方法不足为奇,因为msgSend是有原型导出的,oc对象的方法都运行期绑定的,绑定信息都在objc_class中.只要在调试中[receiver sel]之类,lldb ...
- 【python测试开发栈】python内存管理机制(一)—引用计数
什么是内存 在开始进入正题之前,我们先来回忆下,计算机基础原理的知识,为什么需要内存.我们都知道计算机的CPU相当于人类的大脑,其运算速度非常的快,而我们平时写的数据,比如:文档.代码等都是存储在磁盘 ...
- 微信中使用popup等弹窗组件时点击输入框input键盘弹起导致IOS中按钮无效处理办法
因为在IOS微信中在弹窗中使用input使键盘弹起,使弹窗的位置上移,当键盘关闭时页面还在上面,弹窗位移量也在上面,只有下拉才能回到原位,这样弹窗也消失了.我的处理办法就是在键盘弹起和消失的时候,让页 ...
- Python3 之 列表推导式
列表推导式(又称列表解析式)提供了一种简明扼要的方法来创建列表. 它的结构是在一个中括号里包含一个表达式,然后是一个for语句,然后是 0 个或多个 for 或者 if 语句.那个表达式可以是任意的, ...
- JAVA中的类不要使用$符的原因解析
下面是在Java中标识符的定义 1.标识符由字母.数字.货币符号(¥.$等).连接符号(_等)组成.(这里的字母为Unicode字符集, 而不再局限于传统的26个英文字母.)2.标识符的首字符可以是字 ...