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 ...
随机推荐
- MySQL数据库的自动备份与数据库被破坏后的恢复(3)
[2] 当数据库被修改后的恢复方法 数据库被修改,可能存在着多方面的原因,被入侵.以及相应程序存在Bug等等,这里不作详细介绍.这里将只介绍在数据库被修改后,如果恢复到被修改前状态的方法. 具体和上面 ...
- 【Luogu4299】首都
BZOJ权限题. 洛谷 题目描述 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B ...
- JavaWeb DOM1
一.BOM的概述 browser object modal :浏览器对象模型. 浏览器对象:window对象. Window 对象会在 <body> 或 <frameset> ...
- 面试题常考&必考之--js中的call()和apply()
apply: 接受两个参数,第一个参数是要绑定给this的值,第二个参数是一个参数数组.当第一个参数为null.undefined的时候,默认指向window. call: 第一个参数是要绑定给thi ...
- apicloud直接上传图片
function getPicture() { api.confirm({ title : "提示", msg : "选择图片", buttons : [&qu ...
- Internet History, Technology, and Security(week2)——History: The First Internet - NSFNet
前言: 上周学习了<电子计算机的曙光>,对战时及战后的计算机的历史发展有了更丰富的了解,今天继续coursera的课程,感觉已经有点适应了课程的节奏(除了经常有些奇奇怪怪的词汇看都看不懂@ ...
- 170817关于Listener的知识点
1. Listener 监听器简介 Listener是JavaWeb中三大组件之一.Servlet.Filter.Listener ...
- navigation ObtacleCostFunction源码分析
ObtacleCostFunction 定义了一个ObstacleCostFunction类,继承自Trajectory类,Trajectory类有8个类参 总共有8个类参 double xv_,yv ...
- 网络处理器(Network Processor)
网络处理器(Network Processor,简称NP),又可以称为交换芯片,专用于实现核心交换机高速转发功能. 根据网络处理器会议(Network Processors Conference)的定 ...
- Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file.
w https://linux.die.net/man/1/bash bash - GNU Bourne-Again SHell Description Bash is an sh-compatibl ...