Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1202 Accepted Submission(s): 284
Problem Description
Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this type is said to be balanced:
+ if it is the empty string
+ if A and B are balanced, AB is balanced,
+ if A is balanced, (A) is balanced.
Chiaki can reorder the strings and then concatenate them get a new string t. Let f(t) be the length of the longest balanced subsequence (not necessary continuous) of t. Chiaki would like to know the maximum value of f(t) for all possible t.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤105) -- the number of strings.
Each of the next n lines contains a string si (1≤|si|≤105) consisting of `(' and `)'.
It is guaranteed that the sum of all |si| does not exceeds 5×106.
Output
For each test case, output an integer denoting the answer.
Sample Input
2 1 )()(()( 2 ) )(
Sample Output
4 2
题意:
求 最多匹配 多少个 ()
思路:
贪心的思想:
先把 每个串中 可以匹配的() 统计出来然后
对于剩下的 : 只能是 ))((( 或 ((((( 或 ))))) 这种形式
贪心排序的思想 是
全 左的 ((((((( 放在 全右 ))))) 的前面 使尽可能的多 匹配
把 ))) (((((((( 右括号少的 先放在最前面, 后面放 ))))))))(((((( 右括号多的, 使它 尽可能的多匹配 ()
代码:
L 代表 右括号, R 代表左括号
#include
#include
#include
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#pragma comment(linker, "/STACK:1024000000,1024000000") // 扩栈
//next_permutation(a+1,a+x) 全排列
#define rep(i,a,n) for(int i=a;i=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef vector VI;
typedef long long ll;
typedef pair PII;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};
inline void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
inline ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
inline ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
inline ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
inline ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
inline ll inv2(ll b){return qpow(b,MOD-2);}
/*********************************head************************/
/*
* Author : siz
*/
struct node{
int l,r;
}a[maxn];
vector v1,v2,v3;
char str[maxn];
int cmp(node a,node b)
{
if( a.l >= a.r && b.l < b.r) // A 全右 B 全 左
return 0;
if( a.l < a.r && b.l >= b.r)// A 全左 B 全右
return 1;
if( a.l>=a.r && b.l>=b.r)
return a.r > b.r;
return a.l < b.l;
}
int main()
{
int t,n;
scanf("%d",&t);
while (t--)
{
ll ans = 0;
scanf("%d",&n);
rep(i,1,n+1)
{
stack S;
while(!S.empty())S.pop();
scanf("%s",str);
int len = strlen(str);
rep(j,0,len)
{
if(str[j]=='(')
S.push(str[j]);
else
{
if(!S.empty()&&S.top()=='(')
ans++,S.pop();
else
S.push(str[j]);
}
}
int l = 0, r = 0;
while(!S.empty()) // r 左括号 l 右括号
{
if(S.top()=='(')
r++;
else
l++;
S.pop();
}
a[i].l = l; a[i].r = r;
}
//cout<<"ans is "< now)
{
a[i].l = now;
}
ans += a[i].l;
now -= a[i].l;
now += a[i].r;
}
printf("%lld\n",ans*2);
}
return 0;
}