codeforces Hello 2019 C

本文介绍了一种优化的括号匹配算法,通过剔除不可匹配的括号并使用map进行高效配对,解决了一类括号序列配对问题,旨在创建尽可能多的正确括号序列对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (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

Copy

7
)())
)
((
((
(
)
)

Output

Copy

2

Input

Copy

4
(
((
(((
(())

Output

Copy

0

Input

Copy

2
(())
()

Output

Copy

1

Note

In the first example, it's optimal to construct two pairs: "((     )())" and "(     )".

思路:

括号匹配变形,把可供匹配的只有左括号或者右括号的个数剔出来,最后直接用map找即可,注意形如  ")("  此类不能与别的匹配

代码:

#include<bits/stdc++.h>
using namespace std;
char s[(int)5e5 + 10];
map<int,int> mp;
int ans = 0;
bool is_left(char ch)
{
    if (ch == '(')
        return 1;
    return 0;
}
void solve()
{
    stack<char> sk;
    int len = strlen(s),r = 0;
    for (int i = 0;i < len;i ++)
    {
        if (is_left(s[i])) sk.push(s[i]);
        else
        {
            if (!sk.empty())
                sk.pop();
            else r --;
        }
    }
    if (sk.size() && r) return ;
    int temp = r + sk.size();
    if (!temp) ans ++;
    else
        if (!mp.count(temp)) mp[temp] = 1;
        else mp[temp] ++;
}
int main()
{
    int n;
    scanf("%d",&n);
    for (int i = 0;i < n;i ++)
    {
        scanf("%s",s);
        solve();
    }
    map<int,int> :: iterator it;
    ans /= 2;
    for (it = mp.begin();it != mp.end();it ++)
    {
        int x = it -> first,y = it -> second;
        ans + = min(y,mp[-x]);
        mp[x] -= z,mp[-x] -= z;
    }
    printf("%d\n",ans);
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值