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. Docker 学习记录(基础命令)

    1. 获取镜像 docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]  ===>   docker pull ubuntu:16:04 2.运 ...

  2. hadoop排序 -- 全排序

    目录 一.关于Reducer全排序 1.1. 什么叫全排序 1.2. 分区的标准是什么 二.全排序的三种方式 2.1. 一个Reducer 2.2. 自定义分区函数 2.3. 采样 一.关于Reduc ...

  3. string函数库的原型

    #ifndef __HAVE_ARCH_STRCPY /** * strcpy - Copy a %NUL terminated string * @dest: Where to copy the s ...

  4. python爬虫同时输出两个列表(zip函数)

    简介:在做爬虫时,xpath返回的是列表格式,我们又需要将列表中的元素一一对应并存放至字典中,这是就可以用zip函数. zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组, ...

  5. 20155213 2016-2017-2 《Java程序设计》第一周学习总结

    20155213 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 了解JVM.JRE与JDK,并下载.安装.测试JDK JVM JVM是Java Virtua ...

  6. 20155234 2006-2007-2 《Java程序设计》第4周学习总结

    20155234 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 为了避免重复的行为定义使用继承. 要学会如何正确判断使用继承的时机以及继承之后如何活用多态. ...

  7. 考研编程练习---StringMatching(后缀表达式)

    题目描述: Finding all occurrences of a pattern in a text is a problem that arises frequently in text-edi ...

  8. day4 边缘检测Canny

    1.canny边缘检测 # coding=utf-8 import cv2 import numpy as np filename = 'woman.JPEG' #读入图像,以灰度格式 img = c ...

  9. springboot-vue-JWT使用

    springboot-vue-JWT使用 后端引入依赖: <dependency> <groupId>io.jsonwebtoken</groupId> <a ...

  10. Drupal7 实现like(点赞)功能

    尝试了好几个模块做下总结: 1. Like Dislike Buttons 好处:代码实现简单,一看就懂,开启后无需任何配置,自动在node底部显示like和unlike的小手.而且模版改起来也容易. ...