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 ...
随机推荐
- 封装storage
export const local = { getItem(key) { let value = localStorage.getItem(key) if (/^\{.*\}$/.test(valu ...
- LeetCode--102--二叉树的层次遍历(python)
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- linux运维、架构之路-Kickstart无人值守
一.PXE介绍 PXE全名Pre-boot Execution Environment,预启动执行环境:通过网络接口启动计算机,不依赖本地存储设备或本地已安装的操作系统:Client ...
- Rosetta Stone 不在C盘安装步骤
本文出自:http://www.cnblogs.com/2186009311CFF/p/7500637.html Rosetta Stone默认安装在C盘的,很不好,故找到次解决方案: 总体就是移动文 ...
- go语言实战 摘抄
append 函数append会智能地处理底层数组的容量增长.在切片的容量小于1000个元素时,总是会成倍地增加容量.一旦元素个数超过1000,容量的增长因子就会设为1.25, 也就是每次增加25%的 ...
- sh_05_超市买苹果
sh_05_超市买苹果 # 1. 定义苹果的单价 price = 8.5 # 2. 挑选苹果 weight = 7.5 # 3. 计算付款金额 money = weight * price # 4. ...
- IE等浏览器兼容问题解决方案
<meta http-equiv="X-UA-Compatible" content="IE=100" /> 在<head>标签中添加.
- nodejs工作大全
1.修改文件夹中图片的名称 var fs = require('fs');var fileDirectory = "F:\\zdw\\修改文件夹名称\\newFile";var n ...
- hypermesh中怎么设置支反力(反作用力)
Analysis page >> Control cards >> Global output request 勾选 SPCF 和 GPFORCE .
- Hibernate入门简介
什么是Hibernate框架? Hibernate是一种ORM框架,全称为 Object_Relative DateBase-Mapping,在Java对象与关系数据库之间建立某种映射,以实现直接存取 ...