C. 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, if si+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).

题意,寻找合法的括号匹配对数(1,3)和(3,1)不同

题解:感谢llw大佬讲题,思路是将每一组括号转化成对应的值来匹配,计算可匹配的值,相加即可得到答案

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+;
typedef long long ll;
ll n,mx=;
char str[maxn];
map<ll,ll> mp;
int main(){
ios::sync_with_stdio(false);
cin>>n;
while(n--){
cin>>str;
ll flag=,k=,len=strlen(str);
for(int i=;i<len;i++){
if(str[i]=='(') k++;
else k--;
//k=0的情况是这组数据符合要求
//k>0的情况是左大于右
//k<0的情况是右大于左
if(k<) flag=min(flag,k);
}
if(flag<&&k>flag) continue; //不合法的序列不用管,譬如())(
else{
//cout<<k<<" "<<flag<<endl;
mp[k]++;
mx=max(k,mx);
}
}
ll ans=;
for(int i=;i<=mx;i++){
ans+=mp[i]*mp[-i];
}
cout<<ans<<endl;
return ;
}

code forces 990C的更多相关文章

  1. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  2. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  3. Code Forces 833 A The Meaningless Game(思维,数学)

    Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...

  4. Code Forces 543A Writing Code

    题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...

  5. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  6. code forces 382 D Taxes(数论--哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  7. code forces Watermelon

    /* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...

  8. code forces Jeff and Periods

    /* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostrea ...

  9. Code Forces Gym 100971D Laying Cables(单调栈)

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. HTTP协议中request报文请求方法和状态响应码

    一个HTTP请求报文由4部分组成: 请求行(request line) 请求头部(header) 空行 请求数据 下图给出了请求报文的一般格式: 请求行中包括了请求方法,常见的请求方法有: GET:从 ...

  2. PHP使用redis(一)

    1,connect 描述:实例连接到一个Redis.参数:host: string,port: int返回值:BOOL 成功返回:TRUE;失败返回:FALSE <?php  $redis = ...

  3. 22.1 高级函数【JavaScript高级程序设计第三版】

    函数是JavaScript 中最有趣的部分之一.它们本质上是十分简单和过程化的,但也可以是非常复杂和动态的.一些额外的功能可以通过使用闭包来实现.此外,由于所有的函数都是对象,所以使用函数指针非常简单 ...

  4. 【PHP项目】$_SEVER详解

    $_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERVER['REMOTE_ADDR'] //当前用户 IP . $_SERVER['REMOTE_HOST'] ...

  5. python flask学习第2天 URL中两种方式传参

    新创建项目   自己写个url映射到自定义的视图函数 在url中传递参数 app.py from flask import Flask app = Flask(__name__) @app.route ...

  6. stark组件(11):组合搜索

    效果图: 新增函数和类 Option 获取字段的对象或元组 SearchGroupRow 封装数据,展示到前端 get_search_group 获取组合搜索的字段 get_search_group_ ...

  7. JRE和JDK区别

    JRE: Java Runtime Environment JDK:Java Development Kit JRE顾名思义是java运行时环境, 包含了java虚拟机,java基础类库. 是使用ja ...

  8. MySQL CONCAT()与GROUP_CONCAT()的使用

      1 . MySQL CONCAT(str1,str2, ...)  --返回连接的字符串 mysql> SELECT CONCAT('My', 'S', 'QL'); -> 'MySQ ...

  9. 【转】android makefile文件分析

    Makefile的规则如下: target ... : prerequisites ... command ... ... target可以是一个目标文件,也可以是Object File(例如hell ...

  10. 《Cracking the Coding Interview》——第8章:面向对象设计——题目4

    2014-04-23 18:17 题目:设计一个停车位的类. 解法:停车位,就要有停车.取车的功能了.另外我还加了一个工作线程用于计费,每秒给那些有车的车位加1块钱费用. 代码: // 8.4 Des ...