ACM思维题训练集合

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

模拟稍微有一下就可以了

#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
char ch = getchar();
x = 0;
t f = 1;
while (ch < '0' || ch > '9')
f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
x *= f;
} #define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define rep(m, n, i) for (int i = m; i < n; ++i)
#define rrep(m, n, i) for (int i = m; i > n; --i)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
#define N 1005
#define fil(a, n) rep(0, n, i) read(a[i])
//---------------https://lunatic.blog.csdn.net/-------------------//
map<LL, LL> mp;
char con[N];
int main()
{
LL i, p, j, n, check;
LL cont = 0, ans = 0, len1, len2;
scanf("%lld", &n);
getchar();
for (j = 1; j <= n; j++)
{
p = check = 0;
len1 = len2 = 0;
memset(con, 0, sizeof(0));
scanf("%s", con);
for (i = 0; i < 300009; i++)
{
if (con[i] == 0)
break;
if (con[i] == '(')
{
len1++;
p++;
}
else
{
p--;
if (len1)
len1--;
else
len2++;
}
}
if (len1 == 0 && len2 == 0)
cont++;
else
{
if (len1 == 0)
mp[p]++;
if (len2 == 0)
mp[p]++;
}
}
ans = cont * cont; map<LL, LL>::iterator it1;
for (it1 = mp.begin(); it1 != mp.end(); it1++)
{
if (it1->first > 0)
break;
if (mp[-(it1->first)] > 0)
ans += (it1->second) * mp[-(it1->first)];
}
printf("%lld\n", ans);
return 0;
}

CF思维联系– Codeforces-990C Bracket Sequences Concatenation Problem(括号匹配+模拟)的更多相关文章

  1. Bracket Sequences Concatenation Problem括号序列拼接问题(栈+map+思维)

    A bracket(括号) sequence is a string containing only characters "(" and ")".A regu ...

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

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

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

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

  4. CF思维联系–CodeForces -224C - Bracket Sequence

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

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

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

  6. CF思维联系--CodeForces - 218C E - Ice Skating (并查集)

    题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...

  7. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  8. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...

  9. CF思维联系–CodeForces - 223 C Partial Sums(组合数学的先线性递推)

    ACM思维题训练集合 You've got an array a, consisting of n integers. The array elements are indexed from 1 to ...

随机推荐

  1. js中相关的windows方法的使用和location的先关方法的使用

    下面是关于windows的相关方法的简单介绍. setInterval():它有一个返回值,主要是提供给clearInterval使用. setTimeout():它有一个返回值,主要是提供给clea ...

  2. wireshark抓包实战(五),首选项设置和基本的抓包设置

    一.首选项 首选项一般是修改软件底层的一些默认参数 选中编辑,点击首选项按钮 二.抓包选项设置 点击捕获,选中选项 1.捕获网卡设置 2.保存文件方式设置 很多情况下wireshark会保存很大的数据 ...

  3. 如何增加Tomcat内存

    有时候在开发中,常常遇到内存溢出问题,很有可能项目过大,导致Tomcat内存不足问题,那么此时就要给Tomcat添加内存了,如,操作如下 1. 先找到Tomcat目录下的catalina.bat文件

  4. matplotlib 中其他基于Affine2DBase的类

    2020-04-11 11:42:22 --Edit by yangray以下所有类(除了BboxTransformToMaxOnly )都直接继承于Affine2DBaseIdentityTrans ...

  5. 数据结构和算法(Golang实现)(18)排序算法-前言

    排序算法 人类的发展中,我们学会了计数,比如知道小明今天打猎的兔子的数量是多少.另外一方面,我们也需要判断,今天哪个人打猎打得多,我们需要比较. 所以,排序这个很自然的需求就出来了.比如小明打了5只兔 ...

  6. Floyd-例题-实现-我的第一篇博客

    https://www.cnblogs.com/lbssxz/p/11014911.html 这是网上看到的题目,以上是原博主的解答和题目来源(没找到别的题目来源) 题目大意: 城市交通费 [问题描述 ...

  7. Coin Change UVA

    Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to makech ...

  8. 永恒之蓝MS17010复现

    MS17010复现 靶机win7:192.168.41.150 攻击kali:   192.168.41.147 扫描 通过auxiliary/scanner/smb/smb_ms17_010模块扫描 ...

  9. [PHP][thinkphp5] 学习二:路由、配置调用、常量定义调用

    路由: 其实TP5就是一个集多家框架所长而成的,感觉失去了自己的特色!路由这块呢类似于laravel框架!废话不说直接上码! 路由配置,类似laravel,就在route.php文件里配置路由(文件所 ...

  10. DataTable 与XML 交互

    一.将DataTable的内容写入到XML文件中 /// <summary> /// 将DataTable的内容写入到XML文件中 /// </summary> /// < ...