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. 详解scrapy

    >> (1) 基本概念 >> (2) 爬虫与反爬 >> (3) 基本概念 >> (4) 基本概念

  2. SBT构建工具

    SBT Simple Build Tool. A interactive build tool. install windows可直接到http://www.scala-sbt.org/0.13/do ...

  3. sysbench安装for oracle

    RHEL7.2+ 1.依赖包安装 * autoconf * automake * cdbs * debhelper (>= 9) * docbook-xml * docbook-xsl * li ...

  4. 微信小程序如何在使用wx.request使用cookie

    我主要是做asp.net mvc后端开发的,经常使用Jquery的ajax与后台的Web API进行数据交互. 最近公司要做一个小程序,要实现小程序与Web前端的通信,当然小程序是可以实现socket ...

  5. bzoj 1226 学校食堂Dining

    Written with StackEdit. Description 小\(F\) 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜 ...

  6. fpga产生伪随机序列

    1,一位模二加法法则:加减法等同于异或,没有进位. 2,将移位寄存器的某几级作为抽头进行模二加法后作为反馈输入,就构成了有反馈的动态移位寄存器.此方法产生的序列是有周期的. 3,假设移位寄存器的级数为 ...

  7. raw_in_fields

    在admin后台类中加入raw_id_fields(只适用于外键)后,会显示外键的详细信息

  8. 【转】eclipse + Pydev 配置Python开发环境

    原文网址:http://www.cnblogs.com/dflower/archive/2010/05/13/1734522.html 1. 下载并安装python,由于3.1版本貌似存在很多兼容问题 ...

  9. 17.Selenium+Python日期控件小案例

    1.web上的控件种类十分多,但是大致分为2种,一种为类型是input的且可以输入,第二种为类型是input的且属性为readonly,文本框不可编辑 2.第一种类型为可以输入的,直接send_key ...

  10. 创建工程常量 (OC中的宏定义)

    1.oc创建宏 文件 2.swift创建 常量文件 在swift中, 并非是预编译代码替换, 而是设置全局常量, 简单宏, 直接let 加常量名即可