C Yuhao and a Parenthesis
2 seconds
256 megabytes
standard input
standard output
One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.
A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.
Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.
This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it?
The first line contains one integer nn (1≤n≤1051≤n≤105) — the number of bracket sequences.
Each of the following nn lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")".
The sum of lengths of all bracket sequences in the input is at most 5⋅1055⋅105.
Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter.
Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement.
7
)())
)
((
((
(
)
)
2
4
(
((
(((
(())
0
2
(())
()
1
In the first example, it's optimal to construct two pairs: "(( )())" and "( )".
首先保证每个字符串只多(和)其中一个,然后匹配就好了
代码一
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=;
const int MAXL=;
int n,len;
int lef[MAXL],rig[MAXL],ans=;
char str[MAXL];
int main()
{
int num;
bool mid=false;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",str);
len=strlen(str);
num=;
for(int i=;i<len;i++)
{
if(str[i]=='(') num++;
if(str[i]==')') num--;
if(num<) break;
}
if(num==)
{
if(mid) ans++;
mid^=true;
continue;
}
else if(num>)
{
if(rig[num]>)
{
rig[num]--;
ans++;
}
else lef[num]++;
}
num=;
for(int i=len-;<=i;i--)
{
if(str[i]==')') num++;
if(str[i]=='(') num--;
if(num<) break;
}
if(num>)
{
if(lef[num]>)
{
lef[num]--;
ans++;
}
else rig[num]++;
}
}
printf("%d",ans);
return ;
}
代码二
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
const int mx=5e5+;
char str[mx];
int num=;
int sum[mx];
int sum2[mx];
int n;
int ans=;
stack<char>st;
int main()
{
int t;
scanf("%d",&t);
for (int i=;i<=t;++i)
{
scanf("%s",str);
while (!st.empty()) st.pop();
st.push(str[]);
for (int j=;str[j]!='\0';++j)
{
if (!st.empty())
{
if (st.top()=='('&&str[j]==')')
{
st.pop();
}
else st.push(str[j]);
}
else st.push(str[j]);
}
int ls=,rs=;
while (!st.empty())
{
char xx=st.top();
st.pop();
if (xx=='(') ls++;
else rs++;
}
if (ls!=&&rs!=)
{
continue;
}
else if (ls==&&rs==)
{
num++;
}
else if (rs!=)
{
sum2[rs]++;
}
else sum[ls]++;
}
for (int i=;i<=mx-;++i)
{
if (sum[i]>sum2[i])
{
ans+=sum2[i];
}
else ans+=sum[i];
}
ans+=num/;
printf("%d\n",ans);
}
代码三
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=+;
const int MOD=1e9+;
const double PI = acos(-1.0);
const double EXP = 1E-;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,ans;
int a[N];
char str[N];
stack<char>st;
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
scanf("%d",&n);
memset(a,,sizeof(a));
for(int i=;i<=n;i++){
cin>>str;
while (!st.empty()) st.pop();
st.push(str[]);
for (int j=;str[j]!='\0';++j)
{
if (!st.empty())
{
if (st.top()=='('&&str[j]==')')
{
st.pop();
}
else st.push(str[j]);
}
else st.push(str[j]);
}
int ls=,rs=;
while (!st.empty())
{
char xx=st.top();
st.pop();
if (xx=='(') ls++;
else rs++;
}
if (ls!=&&rs!=)
{
continue;
}
else if (ls==&&rs==)
{
a[]++;
}
else if (rs!=)
{
a[-rs]++;
}else a[+ls]++; }
for(int i=;i<=;i++){
ans+=min(a[-i],a[+i]);
}
ans+=a[]/;
cout << ans << endl; return ;
}
C Yuhao and a Parenthesis的更多相关文章
- codeforces 1097 Hello 2019
又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std; cha ...
- Hello 2019 Solution
A. Gennady and a Card Game 签到. #include <bits/stdc++.h> using namespace std; ], t[]; bool solv ...
- Hello 2019题解
Hello 2019题解 题解 CF1097A [Gennady and a Card Game] map大法好qwq 枚举每一个的第\(1,2\)位判是否与给定的重复即可 # include < ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis
1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...
- 2016年省赛G题, Parenthesis
Problem G: Parenthesis Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 398 Solved: 75[Submit][Status ...
- HDU 5831 Rikka with Parenthesis II(六花与括号II)
31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- CSU 1809 Parenthesis(线段树+前缀和)
Parenthesis Problem Description: Bobo has a balanced parenthesis sequence P=p1 p2-pn of length n and ...
- HDU 5831 Rikka with Parenthesis II (栈+模拟)
Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
随机推荐
- Percona Xtrabackup备份mysql大数据库(完整备份与增量备份)
Percona Xtrabackup备份mysql大数据库(完整备份与增量备份) 文章目录 [隐藏] Xtrabackup简介 Xtrabackup安装 Xtrabackup工具介绍 inno ...
- Apache+Mysql+PHP 套件
Apache+Mysql+PHP 套件 最近要装个Apache+Mysql+PHP的一个环境. google下后,发现现在的安装变得越来越简单了.不再需要麻烦的配置安装,只需简单执行个sh就搞定了 ...
- React native 之 Promise
关键词:Promise Promise.all Promise是什么?=> https://www.runoob.com/w3cnote/es6-promise.html Promise.all ...
- telnet ip 端口
telnet ip 端口 1.关闭防火墙, 2.配置防火墙,出入端规则
- BZOJ 1304: [CQOI2009]叶子的染色 树形DP + 结论
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...
- R 大小写转换
>x = "CAGTTTCTTGAGTCTGATTAATTCAGGTTTCGGGGT"#定义字符串变量x>tolower(x)[1] "cagtttcttga ...
- 3D Computer Grapihcs Using OpenGL - 01 环境设置
这系列文章是我学习Youtube上一套OpenGL教程的笔记,自己对教程的案例重新制作并且做了一定程度的修改(更有条理,且修正了一些问题).后续将持续更新. Visual Studio 2017工程 ...
- 尚硅谷Docker---1-5、docker简介
尚硅谷Docker---1-5.docker简介 一.总结 一句话总结: docker是环境打包:有点像windows镜像 docker的实质:缩小版.精细版.高度浓缩版的一个小型的linux系统 1 ...
- JDBC之Statement 接口的测试(存在sql注入风险)
实现简单的登录功能 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; impo ...
- 新手解惑:nginx&php-fpm&fastcgi 是什么关系
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index ...