C. Longest Regular Bracket Sequence
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1

(吐槽:这题真是道好题呀。。想贪心贪心不出来,想dp,转移方程不知从何推起。。。

题意:给一个字符串,有左括号和右括号。求满足括号配对的最长子串的长度及个数。

解题思路1:求dp转移方程。遇到左括号就记录下来,遇到右括号,就把与之最近的左括号取出来(如果有的话)与右括号配对,此时这段的长度是右括号位置与左括号位置之差,记为dp[t]。
此时的这个长度,再加上一个相邻串的长度即dp[t-1]的长度,就是目前这个串的长度啦,所以转移方程就是dp[t]=dp[t-1]+i-t+1;
附ac代码:
 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <algorithm>
5 #include <stack>
6 using namespace std;
7 const int maxn = 1e6+5;
8 int dp[maxn];
9 stack<int>q;
10 int main() {
11 ios::sync_with_stdio(false);
12 cin.tie(0);cout.tie(0);
13 string s;
14 int cnt=1,maxx=0;
15 cin>>s;
16 for(int i=0;i<s.size();++i) {
17 if(s[i]=='(') {
18 q.push(i);
19 }
20 else if(!q.empty()) {
21 int u=q.top();
22 q.pop();
23 dp[i]=dp[u-1]+i-u+1;
24 if(dp[i]>maxx) {
25 maxx=dp[i];
26 cnt=1;
27 }
28 else if(dp[i]==maxx) {
29 cnt++;
30 }
31 }
32 }
33 if(maxx==0) cout<<0<<" "<<1;
34 else cout<<maxx<<" "<<cnt;
35 return 0;
36 }

解题思路2:先找出能够括号匹配的子串,然后判断哪个最长。具体做法是分别从前向后和从后向前遍历一遍,比如从前向后遍历的时候,遇到一个右括号且左括号不为0的时候,说明这个右括号是肯定可以括号匹配的,记录下来dp[i]=1,反向遍历也是同样操作。

然后for循环遍历记录数组,看dp[i]是否为1,找出最长的及其个数,代码比文字更好理解。

附ac代码:

 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <algorithm>
5 #include <stack>
6 using namespace std;
7 const int maxn = 1e6+5;
8 int dp[maxn];
9 stack<int>q;
10 int main() {
11 ios::sync_with_stdio(false);
12 cin.tie(0);cout.tie(0);
13 string s;
14 int cnt=0,maxx=0,ans=0;
15 cin>>s;
16 for(int i=0;i<s.size();++i) {
17 if(s[i]=='(') {
18 ++cnt;
19 }
20 else if(cnt>0) {
21 dp[i]=1;
22 --cnt;
23 }
24 }
25 cnt=0;
26 for(int i=s.size()-1;i>=0;--i) {
27 if(s[i]==')') {
28 ++cnt;
29 }
30 else if(cnt>0) {
31 dp[i]=1;
32 --cnt;
33 }
34 }
35 cnt=1;ans=1;
36 for(int i=0;i<s.size();++i) {
37 if(dp[i]&&dp[i+1]) {
38 ++cnt;
39 }
40 else cnt=1;
41 if(cnt>maxx) {
42 maxx=cnt;
43 ans=0;
44 }
45 if(cnt==maxx) {
46 ++ans;
47 }
48 }
49 if(maxx==1) cout<<0<<" "<<1;
50 else cout<<maxx<<" "<<ans;
51 return 0;
52 }

codeforces 5C的更多相关文章

  1. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  2. Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)

    题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...

  3. CodeForces 5C Longest Regular Backet sequence

    This is yet another problem dealing with regular bracket sequences. We should remind you that a brac ...

  4. CodeForces - 5C(思维+括号匹配)

    题意 https://vjudge.net/problem/CodeForces-5C 给出一个括号序列,求出最长合法子串和它的数量. 合法的定义:这个序列中左右括号匹配. 思路 这个题和普通的括号匹 ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. MYSQL面试题-索引

    MYSQL面试题-索引 引自B站up编程不良人:https://www.bilibili.com/video/BV19y4y127h4 一.什么是索引? 官方定义:索引是一种帮助mysql提高查询效率 ...

  2. GlusterFS数据存储脑裂修复方案最全解析

    本文档介绍了glusterfs中可用于监视复制卷状态的heal info命令以及解决脑裂的方法 一. 概念解析 常见术语 名称 解释 Brick GlusterFS 的基本存储单元,由可信存储池中服务 ...

  3. winform 窗体中顶部标题居中显示

    在网上看了很多例子,都不能居中,都有或多或少的问题 自己根据网友的代码改编入下: 先确随便写一个标题的内容: string titleMsg ="Winfrom Title" 获取 ...

  4. Ajax中的同源政策

    Ajax中的同源政策 Ajax请求限制 Ajax只能向自己的服务器发送请求.比如现在有一个A网站.有一个B网站,A网站中的HTML文件只能向A网站服务器中发送Ajax请求,B网站中的HTML文件只能向 ...

  5. 一文搞定全场景K3s离线安装

    作者简介 王海龙,Rancher中国社区技术经理,负责Rancher中国技术社区的维护和运营.拥有6年的云计算领域经验,经历了OpenStack到Kubernetes的技术变革,无论底层操作系统Lin ...

  6. 图解ARP协议

    前置知识:MAC地址 在说到ARP协议之前,需要了解MAC地址,在OSI七层模型中,第三层是网络层,要解决的是下一跳机制确定的设备位置,具体来说就是网卡地址,MAC地址用于在网络中唯一标示一个网卡,一 ...

  7. The WebSocket Protocol 1000

    https://tools.ietf.org/html/rfc6455 https://tools.ietf.org/html/rfc6455 7.4.1. Defined Status Codes ...

  8. hashCode、equals详解

    hash和hash表是什么? hash是一个函数,该函数中的实现就是一种算法,就是通过一系列的算法来得到一个hash值,这个时候,我们就需要知道另一个东西,hash表,通过hash算法得到的hash值 ...

  9. Java基础图解,JVM,线程,Spring,TCP,SpringMVC等开发体系图解

    Java基础图解,JVM,线程,Spring,TCP,SpringMVC等开发体系图解 1.Java虚拟机运行时数据区图 2. 堆的默认分配图 3.方法区结构图 4.对象的内存布局图 5.对象头的Ma ...

  10. 跨边界传输之反弹shell

    反弹shell     1.nc         正向连接             攻击机                 nc-vv 受害者ip 受害者port             受害者    ...