C Yuhao and a Parenthesis

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 (1n1051≤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 51055⋅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 "(     )".

 

首先保证每个字符串只多(和)其中一个,然后匹配就好了

代码一

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8 const int MAXN=100050;
 9 const int MAXL=500050;
10 int n,len;
11 int lef[MAXL],rig[MAXL],ans=0;
12 char str[MAXL];
13 int main()
14 {
15     int num;
16     bool mid=false;
17     scanf("%d",&n);
18     for(int i=1;i<=n;i++)
19     {
20         scanf("%s",str);
21         len=strlen(str);
22         num=0;
23         for(int i=0;i<len;i++)
24         {
25             if(str[i]=='(') num++;
26             if(str[i]==')') num--;
27             if(num<0) break;
28         }
29         if(num==0)
30         {
31             if(mid) ans++;
32             mid^=true;
33             continue;
34         }
35         else if(num>0)
36         {
37             if(rig[num]>0)
38             {
39                 rig[num]--;
40                 ans++;
41             }
42             else lef[num]++;
43         }
44         num=0;
45         for(int i=len-1;0<=i;i--)
46         {
47             if(str[i]==')') num++;
48             if(str[i]=='(') num--;
49             if(num<0) break;
50         }
51         if(num>0)
52         {
53             if(lef[num]>0)
54             {
55                 lef[num]--;
56                 ans++;
57             }
58             else rig[num]++;
59         }
60     }
61     printf("%d",ans);
62     return 0;
63 }
View Code

代码二

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stack>
 4 using namespace std;
 5 const int mx=5e5+10;
 6 char str[mx];
 7 int num=0;
 8 int sum[mx];
 9 int sum2[mx];
10 int n;
11 int ans=0;
12 stack<char>st;
13 int main()
14 {
15     int t;
16     scanf("%d",&t);
17     for (int i=1;i<=t;++i)
18     {
19         scanf("%s",str);
20         while (!st.empty()) st.pop();
21         st.push(str[0]);
22         for (int j=1;str[j]!='\0';++j)
23         {
24             if (!st.empty())
25             {
26                 if (st.top()=='('&&str[j]==')')
27                 {
28                     st.pop();
29                 }
30                 else st.push(str[j]);
31             }
32             else st.push(str[j]);
33         }
34         int ls=0,rs=0;
35         while (!st.empty())
36         {
37             char xx=st.top();
38             st.pop();
39             if (xx=='(') ls++;
40             else rs++;
41         }
42         if (ls!=0&&rs!=0)
43         {
44             continue;
45         }
46         else if (ls==0&&rs==0)
47         {
48             num++;
49         }
50         else if (rs!=0)
51         {
52             sum2[rs]++;
53         }
54         else sum[ls]++;
55     }
56     for (int i=1;i<=mx-10;++i)
57     {
58         if (sum[i]>sum2[i])
59         {
60             ans+=sum2[i];
61         }
62         else ans+=sum[i];
63     }
64     ans+=num/2;
65     printf("%d\n",ans);
66 }
View Code

代码三

 1 #include <bits/stdc++.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<string>
 8 #include<vector>
 9 #include<bitset>
10 #include<queue>
11 #include<deque>
12 #include<stack>
13 #include<cmath>
14 #include<list>
15 #include<map>
16 #include<set>
17 //#define DEBUG
18 #define RI register int
19 using namespace std;
20 typedef long long ll;
21 //typedef __int128 lll;
22 const int N=1000000+10;
23 const int MOD=1e9+7;
24 const double PI = acos(-1.0);
25 const double EXP = 1E-8;
26 const int INF = 0x3f3f3f3f;
27 int t,n,m,k,q,ans;
28 int a[N];
29 char str[N];
30 stack<char>st;
31 int main()
32 {
33 #ifdef DEBUG
34     freopen("input.in", "r", stdin);
35     //freopen("output.out", "w", stdout);
36 #endif
37     scanf("%d",&n);
38     memset(a,0,sizeof(a));
39     for(int i=1;i<=n;i++){
40         cin>>str;
41         while (!st.empty()) st.pop();
42         st.push(str[0]);
43         for (int j=1;str[j]!='\0';++j)
44         {
45             if (!st.empty())
46             {
47                 if (st.top()=='('&&str[j]==')')
48                 {
49                     st.pop();
50                 }
51                 else st.push(str[j]);
52             }
53             else st.push(str[j]);
54         }
55         int ls=0,rs=0;
56         while (!st.empty())
57         {
58             char xx=st.top();
59             st.pop();
60             if (xx=='(') ls++;
61             else rs++;
62         }
63         if (ls!=0&&rs!=0)
64         {
65             continue;
66         }
67         else if (ls==0&&rs==0)
68         {
69             a[500000]++;
70         }
71         else if (rs!=0)
72         {
73             a[500000-rs]++;
74         }else a[500000+ls]++;
75  
76     }
77     for(int i=1;i<=500000;i++){
78        ans+=min(a[500000-i],a[500000+i]);
79     }
80     ans+=a[500000]/2;
81     cout << ans << endl;
82  
83     return 0;
84 }
View Code

 

posted @ 2019-01-05 16:07  DWVictor  阅读(399)  评论(0编辑  收藏  举报