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--;elselen2++;}}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思维联系–CodeForces -224C - Bracket Sequence

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

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

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

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

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

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

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

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

  7. CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)

    ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...

  8. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

  9. CodeForces - 224C. Bracket Sequence (栈模拟)简单做法

    A bracket sequence is a string, containing only characters "(", ")", "[&quo ...

最新文章

  1. 负载均衡算法-响应速度均衡
  2. 深度报告:一文看懂生物芯片产业
  3. asp oracle 分页显示,利用ASP实现Oracle数据记录的分页显示
  4. hadoop中datanode无法启动,报Caused by: java.net.NoRouteToHostException: No route to host
  5. java正则表达式去除xml标签之间的空格_HTML解析器——htmlparser2使用详解,换个姿势解析html和xml
  6. python数组随机打乱_对Python random模块打乱数组顺序的实例讲解
  7. C++ 不懂的地方 记录01
  8. 窗口的新建移动和改变大小
  9. Angular JS 学习笔记(二)
  10. InnoSetup 制作安装包进阶版
  11. pdf打印机如何加密pdf文件?
  12. 马虎词汇教程21-25(转载)
  13. 云从科技的Pixel-Anchor论文解读
  14. 亨登谈判策略(转载)
  15. VMware 虚拟机安装 Mac OS X Mountain Lion 苹果系统
  16. Java里氏转换_里氏代换原则、依赖倒转原则 | 学步园
  17. 涉案金额600万!微粒贷诈骗团伙被警方一锅端
  18. Android软件架构
  19. 国防科技大学计算机学院微电子,国防科大微电子所在声表面波高频特性研究方面取得重要进展...
  20. 《庄子·外篇·骈拇第八》

热门文章

  1. 菜单自定义图标_操作系统任务栏了解多少,Windows server 2008 R2自定义通知区域...
  2. Django学习(第四天:ORMModel)
  3. Windows学习总结(6)——MindManager新手入门教程
  4. A problem while linking c++ to python
  5. 查看MySQL表占用空间大小
  6. python getopt
  7. 根据实例说数据库设计(一)—— 人员管理
  8. 如何删除子域信任关系?
  9. 给Vista系统加入一键还原功能
  10. 51单片机怎么显示当前时间_单片机初学者必看