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.

Examples
Input
3
)
()
(
Output

 
2
Input

 
2
()
()
Output
4
Note

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) .

题意:有n个字符串,每个字符串都只有'('和')'组成,从中找出两个字符串可以构成完全匹配的个数(这两个字符串也可以由自己本身组成,如(2,2),(1,1).

题解:所有的字符串可以分为3类:1.自身完美匹配型(即左括号和右括号完美匹配)2:除去完全匹配的子串,剩下的都是左括号,3:除去完全匹配的子串,剩下的都是右括号。对于第一类他的个数ans=c(n,2)*A(2,2)+n(它自身构成的完美匹配),对于第二类和第3类,用map查询一遍(如果有左括号的个数等于右括号的个数,ans=(左括号的种类*右括号的种类),最后不要忘记除去2,因为我们算了两遍。还有一点要注意的是一定要用long long ,我错了好几次才发现这一点。

 #include<stdio.h>
#include<string.h>
#include<stack>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
const int MAXN=3e5+;
ll m;
ll ans;
char str[MAXN];
map<ll ,ll>::iterator it;
int main()
{
ll T;
scanf("%lld",&T);
map<ll,ll>mp;
mp.size();
while(T--)
{
stack<char>s;
scanf(" %s",&str);
ll len=strlen(str);
for(ll i=;i<len;i++)
{
if(!s.empty())
{
if(s.top()=='('&&str[i]==')')
{
s.pop();
}
else
s.push(str[i]);
}
else
{
s.push(str[i]);
}
}
if(s.empty())
{
m++;
}
else
{
ll cpp=s.size(),flag=;
while(!s.empty())
{
if(s.top()=='(')
flag++;
s.pop();
}
if(flag==)//栈里面都是右括号
mp[-cpp]++;
else if(flag==cpp)//栈里面都是左括号
{
mp[cpp]++;
}
}
}
for(it=mp.begin();it!=mp.end();it++)
{
ll k=it->first;
if(mp.count(-k)) {
ans+=(ll)(it->second*mp[-k]);//左括号的种类*右括号的种类
}
}
printf("%lld\n",ans/+m*m);
}

Educational Codeforces Round 45 (Rated for Div. 2)的更多相关文章

  1. Educational Codeforces Round 45 (Rated for Div. 2) C、D

      C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 ...

  2. Educational Codeforces Round 45 (Rated for Div. 2) E - Post Lamps

    E - Post Lamps 思路:一开始看错题,以为一个地方不能重复覆盖,我一想值这不是sb题吗,直接每个power check一下就好....复杂度nlogn 然后发现不是,这样的话,对于每个po ...

  3. Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting

    G - GCD Counting 思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦. 终判被卡了MLE.....  需要每次清空一下子树的map... #inc ...

  4. Educational Codeforces Round 45 (Rated for Div. 2) F - Flow Control

    F - Flow Control 给你一个有向图,要求你给每条边设置流量,使得所有点的流量符合题目给出的要求. 思路:只有在所有点的流量和为0时有解,因为增加一条边的值不会改变所有点的总流量和, 所以 ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  7. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  8. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

随机推荐

  1. HTTPPost/AFNetWorking/JSONModel/NSPredicate

    一.HTTPPost================================================ 1. POST方式发送请求 HTTP协议下默认数据发送请求方法是GET方式,若需要 ...

  2. C++中关于标准输出流cout中使用递增运算符的问题

    今天听同学提起一个问题,在C++中运行下面的语句会得到什么结果: ; cout << a++ << a++ << a++; 起初我个人想当然地认为会输出012,然而 ...

  3. Markdown速成班

    更多内容请参考: http://ibruce.info/2013/11/26/markdown/

  4. Arcgis andoid开发之应用百度地图接口实现精准定位与显示

    怀着激动.兴奋的心情,在这个漫天柳絮的季节写下了这片博文,为什么呢,因为困扰我很久的一个技术性的问题得到了解决,发次博文,供大家参观.学习,同时,也以慰藉我长期困扰的心情,好了,废话不再,言归正传,看 ...

  5. 使用LNMP环境安装typecho博客的全程记录

    虽然我是搞asp.net的 但是十分欣赏php,php有很多开源的博客程序 比如大名鼎鼎的Wordpress.还有各种独立博客大牛使用的z-blog,以及短小精悍的emblog. wordpress臃 ...

  6. R+markdown+LaTeX 中文编译解决方案

    一丢丢前言 很久之前曾试图以Rmarkdown编译pdf文档,无奈怎么鼓捣都会error,搜索了很久都没能找到比较好的解决方案.在配置上将编译器调成了xeLaTeX后就不了了之.这两天心血来潮研究了一 ...

  7. BJOI2019 游记

    BJOI 2019 游记 Day 1 开场拿到 \(T1\) 发现可以转成求平均 \(log\) 直接 \(AC\) 自动机上 \(Dp\) 一波即可 \(T2\) 发现是到数论神仙题,大概能想到要用 ...

  8. 遇到eclipse安装插件一直报错问题(版本问题)

    刚好用了一个插件,然后在线安装报错了,一直报错,之前用的这个插件是直接解压贴的,现在在线安装出错,就很尬,我又有点懒,不想再去下载,贴,所以查了下,有网友说是eclipse的插件版本问题,更新了下,成 ...

  9. SqlServer 临时表

    SqlServer中临时表分为两种:一种是局部(本地)临时表,用#TableName表示.一种是全局(服务器)临时表,用##TableName表示. 创建临时表: 1. create table #T ...

  10. matlab中求逆矩阵的高斯消元法实现的代码

    function qiuni =INV_GET(a)N=length(a);M=eye(N);%得到上三角矩?for i=1:N max=a(i,i); A=i; for j=i+1:N if(abs ...