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 ...
随机推荐
- VMware虚拟机添加5个RAID10磁盘并挂载
1.打开虚拟机之前先添加硬盘设备 具体添加方法详见我https://www.cnblogs.com/Ghost-m/p/11707996.html这个博客 制作RAID需要用到madam命令 mdad ...
- 4、Vim编辑器与正则表达式-面试题
题目 自己写答案
- html部分知识点(待完善)
html部分知识点 跳转 在A页面某链接处`<a href="#aa" target="_blank"></a> 在B页面指定的标题处定 ...
- 高性能Web动画和渲染原理系列(5)合成层的生成条件和陷阱
目录 一. 硬件加速相关的几个概念 二. 合成层的生成条件 显式提升 隐式提升 三. 硬件加速的权衡 四. 动画实现的一些建议 示例代码托管在:http://www.github.com/dashno ...
- 如何对 React 函数式组件进行优化
文章首发个人博客 前言 目的 本文只介绍函数式组件特有的性能优化方式,类组件和函数式组件都有的不介绍,比如 key 的使用.另外本文不详细的介绍 API 的使用,后面也许会写,其实想用好 hooks ...
- nyoj 1112 求次数 (map)
求次数 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个新的字符串,i 属于[0,strl ...
- 《JAVA 程序员面试宝典(第四版)》之传递与引用篇
废话开场白 这个周末突然很想创业,为什么呢?原因很简单,我周围的同学或者说玩的比较好的朋友都发达了,前一个月和一个两年前还睡在一张床上的朋友,他现在已经在深圳买房买车了,没错是在深圳买 ...
- python:time模块
(鱼c)time模块详解http://bbs.fishc.com/forum.php?mod=viewthread&tid=51326&extra=page%3D1%26filter% ...
- usaco training <1.2 Your Ride Is Here>
题面 Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often ...
- SpringBoot系列之i18n集成教程
目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...