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
3
)
()
(
Output
2
Input
2
()
()
Output
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).

括号匹配问题,需要注意几个细节。

有以下几种情况是不能与其他组括号构成合法的括号:)(         )(((      )))(

已经是合法的括号组不需要与不合法的括号组匹配。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; #define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r map<LL,LL> mp;
char con[N];
int main()
{
LL i,p,j,n,check;
LL cont=,ans=,len1,len2;
scanf("%lld",&n);
getchar();
for(j=;j<=n;j++)
{
p=check=;
len1=len2=;
memset(con,,sizeof());
scanf("%s",con);
for(i=;i<;i++)
{
if(con[i]==)
break;
if(con[i]=='(')
{
len1++;
p++;
}
else
{
p--;
if(len1)
len1--;
else
len2++;
}
}
if(len1==&&len2==)
cont++;
else
{
if(len1==)
mp[p]++;
if(len2==)
mp[p]++;
}
}
ans=cont*cont; map<LL,LL>::iterator it1;
for(it1=mp.begin();it1!=mp.end();it1++)
{
if(it1->first>)
break;
if(mp[-(it1->first)]>)
ans+=(it1->second)*mp[-(it1->first)];
}
printf("%lld\n",ans);
return ;
}

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. jdbc 3.0

    1.将Blob.Clob类型数据保存到数据库 import java.io.File; import java.io.FileInputStream; import java.io.FileReade ...

  2. 3dContactPointAnnotationTool开发日志(二七)

      今天的主要工作是把选中物体以及复制删除物体和右边三个面板联系起来,就是通过鼠标框选住物体,右边面板的对应项的颜色也会改变,而且通过右边面板也能控制物体的选中状态,被选中的物体成cyan青色,并且包 ...

  3. Fn+F1-F12,避免使用FN+

    用惯了win8.1,再用win10 很不习惯使用FN+F1-F12 如果要避免使用FN+:使用[FN+ESC]

  4. Windows配置java运行环境的步骤

    jdk不同版本下载地址:http://www.oracle.com/technetwork/java/javase/archive-139210.html 1.下载你适合你电脑的jdk版本,链接如上, ...

  5. Dbgrid控件多重选择的怪问题。BookMarkList,BookMark,使用书签,用的ADOQuery控件。100分送上,急阿!!!请高手帮忙!

    Dbgrid控件多重选择的怪问题.BookMarkList,BookMark,使用书签,用的ADOQuery控件.100分送上,急阿!!!请高手帮忙! ( 积分: 100 )<br />p ...

  6. 弱网络模拟测试工具---易测app

    易测功能介绍   易测是一款基于无线客户端研发场景的通用测试工具, 它通过在研发人员的自持机上提供各种辅助能力&标准化的专项测试服务来提升研发质量&效率.   易测app是阿里巴巴做的 ...

  7. C#中的静态常量(const)和动态常量(static和readonly)用法和区别

    C#中有两种常量类型,分别为readonly(运行时常量)与const(编译时常量),本文将就这两种类型的不同特性进行比较并说明各自的适用场景.工作原理 readonly为运行时常量,程序运行时进行赋 ...

  8. SQL中INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL JOIN区别

    sql中的连接查询有inner join(内连接).left join(左连接).right join(右连接).full join(全连接)四种方式,它们之间其实并没有太大区别,仅仅是查询出来的结果 ...

  9. Object 接受集合里面的任意数据类型 所有的类型默认继承object

  10. 使用Xpath定位元素

    1.xpath较复杂的定位方法: 现在要引用id为“J_password”的input元素,可以像下面这样写: WebElement password = driver.findElement(By. ...