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 ...
随机推荐
- sql中count(1)和count(*)有区别吗
count(*) 对 innodb 而言,它需要把数据从磁盘中读取出来然后累计计数:而 MyISAM 引擎把一个表的总行数存在了磁盘上,所以执行 count(*) 会直接返回这个数,如果有 where ...
- 安卓手机和ios手机上图片未设置宽度可能导致ios上图片贼小
处理方法: 设置固定宽度,高度自适应
- electron原来这么简单----打包你的react、VUE桌面应用程序
也许你不甘心只写网页,被人叫做"他会写网页",也许你有项目需求,必须写桌面应用,然而你只会前端,没关系.网上的教程很多,但是很少有能说的浅显易懂的,我尽力将electron打包应用 ...
- java1.8 10大新特性
http://blog.csdn.net/u013598111/article/details/49720867 一.接口的默认方法 Java 8允许我们给接口添加一个非抽象的方法实现,只需要使用 d ...
- JavaScript export
export The export statement is used when creating JavaScript modules to export functions, objects, o ...
- Python字典的json格式化处理(换行与不换行)
Prefer = {"jim": {"War": 1.9, "the big bang": 1.0, "The lord of w ...
- JS-预留字符和转义字符转换
字符实体(Entity) 转义字符(Escape Sequence)也称字符实体 (Character Entity). 定义转义字符串的主要原因是: <和>等符号已经用来表示 HTML ...
- Jmeter源码框架
首先jmeter框架入口类: NewDriver类(src/core/org/apache/jmeter/NewDriver.java) public static void main(String[ ...
- win10+jdk+mysql+tomcat+jpress环境搭建与部署
本机搭建jpress用于接口测试的学习 目录 1.环境与工具准备 2.mysql服务端安装 3.tomcat配置 4.jpress部署 1.环境与工具准备 a.服务器为本机为win10 64位 b.j ...
- PHP 距离我最近排序+二维数组按指定列排序
思路: 1.获取我的位置,即:我的经纬度 2.各站点须有位置 即:排序对象有位置经纬度 3.查询要排序的站点列表 4.循环遍历计算 与我的距离 5.二维数组按 指定列(距离)排序 具体如下: ...