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

Input
  1. 3
    )
    ()
    (
Output
  1. 2
Input
  1. 2
    ()
    ()
Output
  1. 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();
  15. const int N = ;
  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=,ans=,len1,len2;
  26. scanf("%lld",&n);
  27. getchar();
  28. for(j=;j<=n;j++)
  29. {
  30. p=check=;
  31. len1=len2=;
  32. memset(con,,sizeof());
  33. scanf("%s",con);
  34. for(i=;i<;i++)
  35. {
  36. if(con[i]==)
  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==&&len2==)
  53. cont++;
  54. else
  55. {
  56. if(len1==)
  57. mp[p]++;
  58. if(len2==)
  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>)
  68. break;
  69. if(mp[-(it1->first)]>)
  70. ans+=(it1->second)*mp[-(it1->first)];
  71. }
  72. printf("%lld\n",ans);
  73. return ;
  74. }

CodeForces 990C的更多相关文章

  1. Bracket Sequences Concatenation Problem CodeForces - 990C(括号匹配水题)

    明确一下  一个字符串有x左括号不匹配  和 另一个字符串有x个右括号不匹配  这俩是一定能够匹配的 脑子有点迷 emm... 所以统计就好了  统计x个左括号的有几个,x个右括号的有几个 然后 乘一 ...

  2. Codeforces 990C (模拟+组合数学)

    题面: 传送门 分析: 此题O(n2l)" role="presentation" style="position: relative;">O( ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. Java String简单知识点总结

    1.字符串的比较 public void run(){ //str1在池中 String str1 = new String("String"); //str2,str3 存在于堆 ...

  2. web移动端

    h5:低版本(IE8及以下不支持H5标签,要引入html5shiv.js才能正常运行) 条件引入,只是针对PC端,移动端不存在这样的操作 <figure>:专门用来存放图片和相关介绍的 & ...

  3. 小程序获取access_token

    <?php //小程序appid $appid = 'wx79d7c348d19f010c'; //小程序 APPSecret 密钥 $appsecret = 'd624aca86d0350ee ...

  4. 【Linux 命令】- tar 命令

    语法 tar [-ABcdgGhiklmMoOpPrRsStuUvwWxzZ][-b <区块数目>][-C <目的目录>][-f <备份文件>][-F <Sc ...

  5. JavaScript 保留两位小数

    以下我们将为大家介绍 JavaScript 保留两位小数的实现方法: 四舍五入 以下处理结果会四舍五入: var num =2.446242342; num = num.toFixed(2); // ...

  6. Graph-Based image segmentation method

    1.Graph-Based 方法简介 基于图的图像分割方法将图像伪造成带权值无向图的形式 : G = (V, E) 其中,V是顶点集合,把图像中的每个像素或者每个区域看成图的一个顶点:E是边的集合,连 ...

  7. 洛谷P3938 斐波那契

    题目戳 题目描述 小 C 养了一些很可爱的兔子. 有一天,小 C 突然发现兔子们都是严格按照伟大的数学家斐波那契提出的模型来进行 繁衍:一对兔子从出生后第二个月起,每个月刚开始的时候都会产下一对小兔子 ...

  8. NOI2018前的每日记录

    NOI2018前的每日记录 开头 今天是\(2018.7.2\),不知不觉已经这么久了.本来还是高一的小蒟蒻,过不了多久就要成为高二的老年选手了. 再过半个月我也要去\(NOI\)打酱油了.我这种D类 ...

  9. 洛谷 P2530 [SHOI2001]化工厂装箱员 解题报告

    P2530 [SHOI2001]化工厂装箱员 题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B ...

  10. CVPR 2014 ObjectnessBING 原文翻译

    BING: Binarized Normed Gradients for Objectness Estimation at 300fps Ming-Ming Cheng, Ziming Zhang, ...