CF思维联系– Codeforces-990C Bracket Sequences Concatenation Problem(括号匹配+模拟)
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(括号匹配+模拟)的更多相关文章
- Bracket Sequences Concatenation Problem括号序列拼接问题(栈+map+思维)
A bracket(括号) sequence is a string containing only characters "(" and ")".A regu ...
- CF 990C. Bracket Sequences Concatenation Problem【栈/括号匹配】
[链接]:CF [题意]: 给出n个字符串,保证只包含'('和')',求从中取2个字符串链接后形成正确的括号序列的方案数(每个串都可以重复使用)(像'()()'和'(())'这样的都是合法的,像')( ...
- CF990C Bracket Sequences Concatenation Problem 思维 第五道 括号经典处理题目
Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 meg ...
- CF思维联系–CodeForces -224C - Bracket Sequence
ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...
- Bracket Sequences Concatenation Problem CodeForces - 990C(括号匹配水题)
明确一下 一个字符串有x左括号不匹配 和 另一个字符串有x个右括号不匹配 这俩是一定能够匹配的 脑子有点迷 emm... 所以统计就好了 统计x个左括号的有几个,x个右括号的有几个 然后 乘一 ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- CF思维联系– CodeForces - 991C Candies(二分)
ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...
- CF思维联系–CodeForces - 225C. Barcode(二路动态规划)
ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...
- 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 ...
随机推荐
- protobuf总结
1.protobuf是什么? protobuf(protocol buffers)是一种语言中立,平台无关,可扩展的序列化数据的格式,可以用于通信协议,数据存储等. protobuf 相比于xml,j ...
- 计算机网络协议,PPP协议分析
一.基本特点 1.PPP协议是计算机网络体系中第二层(数据链路层)的协议 2.PPP帧格式是以HDLC帧格式为基础,做了很少的改动(区别:PPP是面向字符的,而HDLC是面向位的) 3.PPP协议使用 ...
- js骚操作骂人不带脏
前言 很多小伙伴们觉得javaScript很简单,下面的这行 javaScript代码可能会让你怀疑人生. (!(~+[])+{})[--[~+""][+[]]*[~+[]] + ...
- java解惑之常常忘记的事
java解惑之常常忘记的事 2012-10-17 18:38:57| 分类: JAVA | 标签:基础知识 软件开发 |举报|字号 订阅 针对刚接触java的菜鸟来说,java基础知识 ...
- Docker-Bridge Network 01 容器间通信
本小节介绍bridge network模式下,单机上的容器网络拓扑及通信. 1.前言 对于单机上的容器,Docker提供了bridge.host.none三种网络.我们首先介绍经典的bridge模式. ...
- linux知识点系列之 umask
介绍 umask(user's mask)用来设置文件权限掩码.权限掩码是由3个八进制的数字所组成,将现有的存取权限减掉权限掩码后,即可产生建立文件时预设的权限. UNIX最初实现时不包含umask命 ...
- 用threejs实现三维全景图
网络上看到了3D全景图,发现threejs里面有一个库竟然可以实现,一下我贴出代码: <!DOCTYPE html> <html> <head> <meta ...
- 如何将SqlServer配置为django的数据源(2.2以后版本)
django-pyodbc-azure 是一个官方推荐的 第三方django数据库支持backend. 根据官网的介绍django-pyodbc-azure 只能支持到 django 2.1.如果涉及 ...
- JavaScript_Array
Array 概念特点 值的有序集合: 每一个值叫一个元素: 每个元素在数组中有一个位置,以数字表示,称为索引(下标): 元素可以是任何类型 索引从0开始,最大为2的32次方 数组的创建 数组直接量 v ...
- System类的两个静态方法currentTimeMillis 和 arraycopy
package com.yhqtv.demo02.ThreadPool; import java.util.Arrays; public class Test { public static void ...