【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)
题目
传送门:QWQ
分析
洛谷题解里有一位大佬讲的很好。
就是先用栈预处理出可以匹配的左右括号在数组中设为1
其他为0
最后求一下最长连续1的数量。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
char s[maxn];
int vis[maxn], Stack[maxn], top;
int main(){
scanf("%s",s+);
int n=strlen(s+);
for(int i=;i<=n;i++){
if(s[i]=='('){ Stack[++top]=i; }
else if(top){
vis[i]=; vis[Stack[top]]=; top--;
}
}
int preans=,ans=;
for(int i=;i<=n;i++){
if(vis[i]) preans++;
else preans=;
ans=max(ans,preans);
}
preans=;
int tot=;
for(int i=;i<=n;i++){
if(vis[i]) preans++;
else preans=;
if(preans==ans && ans!=) tot++;
}
if(!(ans==&&tot==)) printf("%d %d",ans,tot);
else puts("0 1");
return ;
}
【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)的更多相关文章
- 【Codeforces】CF 2 B The least round way(dp)
题目 传送门:QWQ 分析 求结尾0的数量QwQ. 10只能是$ 2 \times 5 $,我们预处理出每个数因子中2和5的数量. 我们接着dp出从左上到右下的经过的最少的2的数量和最少的5的数量.两 ...
- 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 ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence
题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence(思维)
传送门 题意: 给你一个只包含 '(' 和 ')' 的长度为 n 字符序列s: 给出一个操作:将第 i 个位置的字符反转('(' ')' 互换): 问有多少位置反转后,可以使得字符串 s 变为&quo ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- 【BZOJ】1617: [Usaco2008 Mar]River Crossing渡河问题(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1617 裸dp,很好做. 设f[i]表示i头牛到对岸所需最小时间.sum[i]表示运i头牛到对岸的时间 ...
- 【BZOJ】1618: [Usaco2008 Nov]Buying Hay 购买干草(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1618 裸的01背包,注意背包的容量不是v即可. #include <cstdio> #i ...
- 【BZOJ】1652: [Usaco2006 Feb]Treats for the Cows(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1652 dp.. 我们按间隔的时间分状态k,分别为1-n天 那么每对间隔为k的i和j.而我们假设i或者 ...
随机推荐
- Ubuntu 12.04硬盘安装教程
从服务器下载Ubuntu 12.04光盘镜像文件到 C 盘.下载地址:\\192.167.100.225\share\Tool\Ubuntu\ubuntu-12.04.1-desktop-amd64. ...
- Linux 之 tar和nc传文件
前提: 将 172.16.88.1 上的 test 文件夹传送到 172.16.88.2 上 接收方 ---- 172.16.88.2 $ nc -l > test.tar 9876 为端口号 ...
- HDU 4557
http://acm.hdu.edu.cn/showproblem.php?pid=4557 解决一类问题的set用法 #include <iostream> #include <c ...
- Hign-Speed Tracking with Kernelzied Correlation Filters
reference:Hign-Speed Tracking with Kernelzied Correlation Filters questions: The core componet of mo ...
- Quartz(一):Cron表达式
正文 1. 表达式介绍 Cron:cron表达式用于配置cronTrigger的实例.cron表达式实际上是由七个子表达式组成.这些表达式之间用空格分隔. Cron格式:[秒] [分] [小时] [日 ...
- CTF-练习平台-Misc之 Linux??????
八.Linux?????? 下载文件,解压后只得到一个没有后缀名的文件,添加后缀名为txt,打开搜索,关键词为“flag”,没有找到:改关键词为“key”得到答案
- hdu 5183 hash表
BC # 32 1002 题意:给出一个数组 a 和一个数 K ,问是否存在数对( i , j ),使 a i - a i + 1 +……+ (-1)j - i a j : 对于这道题,一开始就 ...
- 思维题(两点逼近)LeetCode11 Container with Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode Pascal's Triangle && Pascal's Triangle II Python
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...
- 使用ioctl获取网卡统计信息
ethtool -S获取接口统计信息总共分三步: 1.获取统计项个数,使用SIOCETHTOOL+ETHTOOL_GSSET_INFO 2.(可选)获取统计项名字,使用SIOCETHTOOL+ETHT ...