Bracket Sequences Concatenation Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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, ifsi+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

Copy
3
)
()
(
output

Copy
2
input

Copy
2
()
()
output

Copy
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个只包含'('和')'的字符串,问每两个字符串相互组合后形成的完整括号(由左到右都可以匹配)的种数

首先消除每个字符串内的已匹配的括号,如果剩余的字符串还同时含有左括号和右括号,那么这种字符串是不可以和其他字符串匹配成功的,直接剔除。

最后枚举只剩余左括号的,看有多少右括号与之对应,这样避免了重复计数。

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e6 + ;
const int mod = 1e9 + ;
typedef long long ll;
string s[maxn];
map< pair< ll, ll >, ll > mm;
ll le[maxn], ri[maxn], vis[maxn];
int main(){
std::ios::sync_with_stdio(false);
ll n;
while( cin >> n ) {
mm.clear();
memset( le, , sizeof(le) );
memset( ri, , sizeof(ri) );
memset( vis, , sizeof(vis) );
for( ll i = ; i < n; i ++ ) {
cin >> s[i];
ll l = , r = ;
for( ll j = ; j < s[i].length(); j ++ ) {
if( s[i][j] == '(' ) {
l ++;
} else if( s[i][j] == ')' && l > ) {
l --;
} else if( s[i][j] == ')' && l == ) {
r ++;
}
}
le[i] = l, ri[i] = r;
}
for( ll i = ; i < n; i ++ ) {
if( le[i] && ri[i] ) {
vis[i] = ;
}
}
for( ll i = ; i < n; i ++ ) {
if( !vis[i] ) {
mm[make_pair(le[i],ri[i])] ++;
}
}
ll ans = ;
for( ll i = ; i < n; i ++ ) {
if( !vis[i] && !ri[i] ) {
ans += mm[make_pair(,le[i])];
}
}
cout << ans << endl;
}
return ;
}

最新文章

  1. Python-18-Django 基础篇
  2. HTML CSS SPRITE 工具
  3. JAVA join()方法
  4. Linux服务器的那些性能参数指标
  5. 12 哈希表相关类——Live555源码阅读(一)基本组件类
  6. 作用域内优先级及this指针
  7. 图片轮播插件-carouFredSel
  8. iOS8 iPad Warning: Attempt to present &lt;UIImagePickerController:xxxx &gt; on xxxx which is already presenting (null)
  9. Linux文件解压缩详解
  10. POJ2528线段树基础
  11. 杭州电子科技大学Online Judge 之 “确定比赛名次(ID1285)”解题报告
  12. WPF自定义ListBox样式
  13. GOLang(第二篇 发起一个Http请求)
  14. Confluence5.4.4迁移至6.3.1
  15. 6.[leetcode] ZigZag Conversion
  16. 学习笔记TF023:下载、缓存、属性字典、惰性属性、覆盖数据流图、资源
  17. PersistenceContext.properties()
  18. Navicat for MySQL安装工具及破解工具
  19. Oracle数据类型与.NET中的对应关系
  20. What Your Computer Does While You Wait.CPU的等待有多久?

热门文章

  1. 8天入门docker系列 —— 第八天 让程序跑在swarm集群上
  2. 在vue中监听storage的变化
  3. adb 下载安装
  4. 什么?小程序实时语音识别你还在痛苦的对接科大讯飞?百度Ai识别?
  5. 用CSS来定义&lt;p&gt;标签,要求实现以下效果:字体颜色再IE6下为黑色,IE7下为红色,IE8下为绿色,其他浏览器下为黄色。
  6. (16)ASP.NET Core 通用主机(HostBuilder)
  7. Nginx 1.15.5: 405 Not Allowed
  8. Stream和方法引用
  9. Yii CGridView 之 SQL 语句
  10. js 设计模式——状态模式