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. MSSQL数据库分区表

    http://blog.csdn.net/lgb934/article/details/8662956 http://database.9sssd.com/mssql/art/951

  2. PostgreSQL java读取bytes字段

    写入bytea: File img = new File("/tmp/eclipse.png"); fin = new FileInputStream(img); con = Dr ...

  3. SVN 用户名切换

    方法1:主要针对Window下安装了客户端设置 右键桌面-->settings-->Saved Data -->Authentication data -->clear 方法2 ...

  4. volatile与const综合分析

    在C/C++ 编程中,volatile与const关键字一向容易让人困惑,当然,新手可能从来不用,但是 在高质量和稳健的程序中,这两个关键字 是相当重要的. 相比const,volatile关键字的发 ...

  5. 使用pdfcrack破解PDF密码(Linux)

    pdfcrack是破解PDF保护密码的Linux命令行工具. 安装pdfcrack Debian系列: # apt install pdfcrack 暴力破解 # pdfcrack -f filena ...

  6. Tomcat启动超时问题Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds

    使用Eclipse启动Tomcat时出现启动超时的问题如下所示: Server Tomcat v7.0 Server at localhost was unable to start within 4 ...

  7. 解决direct2d拖拽窗口闪烁

    响应WM_ERASEBKGND,在OnEraseBkgnd()处返回FALSE,阻止GDI重绘客户区背景色,设置背景色的工作交给Direct2D在Render时设置,否则在Resize时会出现窗口闪烁 ...

  8. springmvc中拦截器与springmvc全局异常处理器的问题

    最近在做一个练手的小项目, 系统架构中用了springmvc的全局异常处理器, 做了系统的统一异常处理. 后来加入了springmvc的拦截器, 为了一些需求, 在拦截器中的 preHandle 方法 ...

  9. Log4j日志配置说明

    一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...

  10. ⑤SpringBoot之定时任务

    本文介绍SpringBoot定时任务的使用,springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现. 1.pom配置文件 pom包里面只需要引入springboot starter包 ...