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 n bracket sequences s1,s2,…,sn. Calculate the number of pairs i,j(1≤i,j≤n) such that the bracket sequence si+sj is a regular bracket sequence. Operation + means concatenation i.e. "()(" + ")()" = "()()()".
If si+sj and sj+si are regular bracket sequences and i≠j, then both pairs (i,j) and (j,i) must be counted in the answer. Also, if si+si is a regular bracket sequence, the pair (i,i) must be counted in the answer.
Input

The first line contains one integer n(1≤n≤3⋅105)— the number of bracket sequences. The following n lines contain bracket sequences — non-empty strings consisting only of characters "(" and ")". The sum of lengths of all bracket sequences does not exceed 3⋅105
.
Output
In the single line print a single integer — the number of pairs i,j(1≤i,j≤n)
such that the bracket sequence si+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)and (2,2)
.
In the second example, any pair is suitable, namely (1,1),(1,2),(2,1),(2,2)
.

题目意思:有n个字符串,每个字符串都只有'('和')'组成,从中找出两个字符串si,sj( i ! = j)可以构成完全匹配的个数,同样如果si自身也能完全匹配也要算进去。

解题思路:所有的字符串可以分为3类:

1:  自身完美匹配型(即左括号和右括号完美匹配)

2:除去完全匹配的子串,剩下的都是左括号。

3:除去完全匹配的子串,剩下的都是右括号。

对于第一类他的个数ans=c(n,2)*A(2,2)+n(它自身构成的完美匹配),对于第二类和第3类,用map查询一遍(如果有左括号的个数等于右括号的个数,ans=(左括号的种类*右括号的种类),最后不要忘记除去2,因为我们算了两遍。

 #include<cstdio>
#include<cstring>
#include<map>
#include<stack>
#include<algorithm>
#define ll long long int
#define MAX 300010
using namespace std;
map<ll,ll>mp;
char str[MAX];
int main()
{
ll i,n,len,m,k;
ll counts,ans,sum;
scanf("%lld",&n);
getchar();
m=;
ans=;
while(n--)
{
stack<char>s;
scanf("%s",str);
len=strlen(str);
for(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
{
counts=s.size();
sum=;
while(!s.empty())
{
if(s.top()=='(')
{
sum++;///记录左括号个数
}
s.pop();
}
if(sum==)///剩下的都是右括号
{
mp[-counts]++;///负数代表右括号
}
else if(sum==counts)///栈里剩下的都是左括号
{
mp[counts]++;///正数代表左括号
}
}
}
map<ll,ll>::iterator it;
for(it=mp.begin(); it!=mp.end(); it++)
{
k=it->first;
if(mp.count(-k))///只有存在左括号数等于右括号数的才存在完美匹配
{
ans+=(ll)(it->second)*mp[-k];
}
}
printf("%lld\n",ans/+m*m);
return ;
}

Bracket Sequences Concatenation Problem括号序列拼接问题(栈+map+思维)的更多相关文章

  1. CF990C Bracket Sequences Concatenation Problem 思维 第五道 括号经典处理题目

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

  2. CF思维联系– Codeforces-990C Bracket Sequences Concatenation Problem(括号匹配+模拟)

    ACM思维题训练集合 A bracket sequence is a string containing only characters "(" and ")" ...

  3. CF 990C. Bracket Sequences Concatenation Problem【栈/括号匹配】

    [链接]:CF [题意]: 给出n个字符串,保证只包含'('和')',求从中取2个字符串链接后形成正确的括号序列的方案数(每个串都可以重复使用)(像'()()'和'(())'这样的都是合法的,像')( ...

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

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

  5. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

  6. uoj problem 31 猪猪侠再战括号序列

    题目大意: 给定一个长度为2n的括号序列.定义一个关于区间[l,r]的翻转操作为位置平移对调. 即翻转")))()("可以得到"()()))((" 用不超过n次 ...

  7. B. Dispersed parentheses 记忆化搜索 + 括号序列的状压表示

    http://codeforces.com/gym/100633/problem/B B. Dispersed parentheses time limit per test 2 seconds me ...

  8. 【BZOJ】2209: [Jsoi2011]括号序列(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2209 splay又犯逗........upd1那里的sum忘记赋值反............. 本题 ...

  9. uoj #31. 【UR #2】猪猪侠再战括号序列 贪心

    #31. [UR #2]猪猪侠再战括号序列 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/31 Descript ...

随机推荐

  1. BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)

    Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 587  Solved: 259[Submit][Status][Discuss] Description ...

  2. hdu Exponentiation高精度实数乘幂(用了带小数的高精度模板)

    #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #i ...

  3. VS2015菜单栏重复删除

    举个例子,这个是工具栏的,出现了重复 只要选择工具栏自定义那个选项,在多余命令的下方,先删除几个外部命令,然后把空行删除,最后全部重置即可 结果如下图

  4. 【postgresql的使用】

    #安装: #初始化: #允许远程登录: #创建数据库并指定用户 #创建用户 #列出数据库 #进入数据库 #查询数据 #or(或)查询 #and ,or(和,或查询) #表连接 #内,外(左右),交叉连 ...

  5. 实验吧 Fair Play

    知识点:playfair密码以前没见过 Playfair密码(英文:Playfair cipher 或 Playfair square)是一种使用一个关键词方格来加密字符对的加密法,1854年由查尔斯 ...

  6. python 将歌词解析封装成类,要求:提供一个方法(根据时间返回歌词) - 提示:封装两个类:歌词类、歌词管理类

    自己写的 有更好方案的大佬可以讨论一下 import bisectclass Lrc(): def __init__(self, sec, lrc): self.sec = sec self.lrc ...

  7. PHP中使用Redis接管文件存储Session详解

    前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用redis替换文件来存储session. 最近就遇到了这个问题,之前找了网上的一套直播 ...

  8. 20155336 《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    20155336 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Jav ...

  9. Centos安装man功能

    CentOS接触很久了,但是一直作为服务器端使用.这次之所以安装man,是由于开始学习Nginx了. 言归正传,安装man,首先得下载包.由于我们天朝的原因,代码包几乎下不到的.首先你得FQ,再下载, ...

  10. 对PostgreSQL数据库的hstore类型建立GisT索引的实验

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@g ...