CodeForces 990C
Posted daybreaking
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 990C相关的知识,希望对你有一定的参考价值。
Description
A bracket sequence is a string containing only characters "(" and ")".
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.
You are given nn bracket sequences s1,s2,…,sns1,s2,…,sn. Calculate the number of pairs i,j(1≤i,j≤n)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence. Operation ++ means concatenation i.e. "()(" + ")()" = "()()()".
If si+sjsi+sj and sj+sisj+si are regular bracket sequences and i≠ji≠j, then both pairs (i,j)(i,j) and (j,i)(j,i) must be counted in the answer. Also, if si+sisi+si is a regular bracket sequence, the pair (i,i)(i,i) must be counted in the answer.
Input
The first line contains one integer n(1≤n≤3?105)n(1≤n≤3?105) — the number of bracket sequences. The following nn lines contain bracket sequences — non-empty strings consisting only of characters "(" and ")". The sum of lengths of all bracket sequences does not exceed 3?1053?105.
Output
In the single line print a single integer — the number of pairs i,j(1≤i,j≤n)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence.
Sample Input
3
)
()
(
2
2
()
()
4
Hint
In the first example, suitable pairs are (3,1)(3,1) and (2,2)(2,2).
In the second example, any pair is suitable, namely (1,1),(1,2),(2,1),(2,2)(1,1),(1,2),(2,1),(2,2).
括号匹配问题,需要注意几个细节。
有以下几种情况是不能与其他组括号构成合法的括号:)( )((( )))(
已经是合法的括号组不需要与不合法的括号组匹配。
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<stack> 8 #include<deque> 9 #include<map> 10 #include<iostream> 11 using namespace std; 12 typedef long long LL; 13 const double pi=acos(-1.0); 14 const double e=exp(1); 15 const int N = 300010; 16 17 #define lson i << 1,l,m 18 #define rson i << 1 | 1,m + 1,r 19 20 map<LL,LL> mp; 21 char con[N]; 22 int main() 23 { 24 LL i,p,j,n,check; 25 LL cont=0,ans=0,len1,len2; 26 scanf("%lld",&n); 27 getchar(); 28 for(j=1;j<=n;j++) 29 { 30 p=check=0; 31 len1=len2=0; 32 memset(con,0,sizeof(0)); 33 scanf("%s",con); 34 for(i=0;i<300009;i++) 35 { 36 if(con[i]==0) 37 break; 38 if(con[i]==‘(‘) 39 { 40 len1++; 41 p++; 42 } 43 else 44 { 45 p--; 46 if(len1) 47 len1--; 48 else 49 len2++; 50 } 51 } 52 if(len1==0&&len2==0) 53 cont++; 54 else 55 { 56 if(len1==0) 57 mp[p]++; 58 if(len2==0) 59 mp[p]++; 60 } 61 } 62 ans=cont*cont; 63 64 map<LL,LL>::iterator it1; 65 for(it1=mp.begin();it1!=mp.end();it1++) 66 { 67 if(it1->first>0) 68 break; 69 if(mp[-(it1->first)]>0) 70 ans+=(it1->second)*mp[-(it1->first)]; 71 } 72 printf("%lld ",ans); 73 return 0; 74 }
以上是关于CodeForces 990C的主要内容,如果未能解决你的问题,请参考以下文章
Bracket Sequences Concatenation Problem CodeForces - 990C(括号匹配水题)
[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段