time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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?

Input

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.

Output

Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement.

Examples
input
7
)())
)
((
((
(
)
)
output
2
input
4
(
((
(((
(())
output
0
input
2
(())
()
output
1
Note

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 ;
}

最新文章

  1. MySQL数据库备份--mysqldump用法
  2. JavaScript从父页面获取子页面的值(子页面又如何访问父页面)
  3. hrbust1841再就业(状态压缩dp)
  4. Web Essentials之Markdown和自定义编辑器(Web Essentials完结)
  5. 云计算的三层SPI模型
  6. linux 安装Apache----tar.gz文件安装方式(零环境安装)
  7. Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题
  8. 15、自定义Content Provider
  9. 页面新宠图片格式WebP
  10. JS的事件动态绑定机制
  11. add two nums
  12. windows做代理服务器让内部linux上网
  13. python关于二分查找
  14. git 入门教程之基本概念
  15. day15 十五、模块、from导入、起别名
  16. VueThink配置
  17. WebDriver高级应用实例(7)
  18. Django 添加应用
  19. Unix中Signal信号的不同
  20. Scalable IO in Java【java高效IO】

热门文章

  1. linux运维、架构之路-xtrabackup
  2. [CF1177B]Digits Sequence (Hard Edition)题解
  3. HDU 6287 Just h-index
  4. C++ 对象间通信框架 V2.0 &#215;&#215;&#215;&#215;&#215;&#215;&#215; 之(五)
  5. Java网络编程与NIO详解10:深度解读Tomcat中的NIO模型
  6. Mysql中主键与索引
  7. qbzt day5 上午
  8. Gradle 详解
  9. mkdir: 无法创建目录&quot;kk&quot;: 只读文件系统
  10. 函数介绍——MulDiv