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 ;
}

CF990C Bracket Sequences Concatenation Problem 思维 第五道 括号经典处理题目的更多相关文章

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

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

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

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

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

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

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

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

  5. 再来五道剑指offer题目

    再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...

  6. D. Yet Another Subarray Problem 思维 难 dp更好理解

    D. Yet Another Subarray Problem 这个题目很难,我比赛没有想出来,赛后又看了很久别人的代码才理解. 这个题目他们差不多是用一个滑动窗口同时枚举左端点和右端点,具体如下: ...

  7. FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)

    Chinese Postman Problem is a very famous hard problem in graph theory. The problem is to find a shor ...

  8. cf1088D. Ehab and another another xor problem(思维)

    题意 题目链接 系统中有两个数\((a, b)\),请使用\(62\)以内次询问来确定出\((a, b)\) 每次可以询问两个数\((c, d)\) 若\(a \oplus c > b \opl ...

  9. 【cf1272】F. Two Bracket Sequences

    传送门 题意: 给出\(s,t\)两个合法括号序列,现在找到一个长度最小的合法的序列\(p\),使得\(s,t\)都为其子序列. 思路: 考虑\(dp:dp[i][j][d]\)表示第一个串在\(i\ ...

随机推荐

  1. Hystrix超时测试

    package com.cookie.test; import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.Hystr ...

  2. java高并发系列 - 第24天:ThreadLocal、InheritableThreadLocal(通俗易懂)

    java高并发系列第24篇文章. 环境:jdk1.8. 本文内容 需要解决的问题 介绍ThreadLocal 介绍InheritableThreadLocal 需要解决的问题 我们还是以解决问题的方式 ...

  3. 【Vue的路由,SPA概念】

    前言 本章是为了以后实现前端页面的搭建而写的, 重点在于如何实现 单页Web应用 因为相对于以前的传统多页面web,有很大的缺陷. 那么就必须了解一下Vue的路由设置. SPA的概念 总的而言,我们知 ...

  4. 02、安装Linux系统

    1.我们打开VM软件,然后点击“创建新的虚拟机”,选择“经典”选项 2.选中“稍后安装操作系统”单选按钮,然后单击“下一步”按钮 3.将客户机操作系统的类型选择为“Linux”,版本为"Re ...

  5. H5中的history方法Api介绍

    最近公司在做一个微信公众号,看了项目源码,看到项目中用到了history的Api来进行控制浏览器的历史记录及前进/后退键: 下面来跟大家一起来捋捋history的Api方法和使用: history.p ...

  6. String关键字

    关于String和new String()见我写的前一篇博客 String和new String()的区别 1.String的"+"运算 a.String str = " ...

  7. tensorflow学习笔记——多线程输入数据处理框架

    之前我们学习使用TensorFlow对图像数据进行预处理的方法.虽然使用这些图像数据预处理的方法可以减少无关因素对图像识别模型效果的影响,但这些复杂的预处理过程也会减慢整个训练过程.为了避免图像预处理 ...

  8. 【已解决】Https请求—未能创建 SSL/TLS 安全通道

    在做项目的微信推送消息功能时,由于微信并发量大,导致其它第三方接口调用时直接挂掉报错. 问题: 测试工程师做压测,100个线程同时调用微信和XX站的接口,日志报XX站的“请求被中止: 未能创建 SSL ...

  9. .Net 连接FTP下载文件报错:System.InvalidOperationException: The requested FTP command is not supported when using HTTP proxy

    系统环境: Windows + .Net Framework 4.0   问题描述: C#连接FTP下载文件时,在部分电脑上有异常报错,在一部分电脑上是正常的:异常报错的信息:System.Inval ...

  10. c++json构建与解析组件 RapidJSON 没用过永远不会知道有多好用

    参考资料: 官方文档 推荐[腾讯RapidJSON]学习笔记 原理请参考以上资料 构建json Document doc; Document::AllocatorType &allocator ...