CodeForces 990C
Description
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.
Sample Input
- 3
)
()
(
- 2
- 2
()
()
- 4
Hint
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).
括号匹配问题,需要注意几个细节。
有以下几种情况是不能与其他组括号构成合法的括号:)( )((( )))(
已经是合法的括号组不需要与不合法的括号组匹配。
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- #include<queue>
- #include<stack>
- #include<deque>
- #include<map>
- #include<iostream>
- using namespace std;
- typedef long long LL;
- const double pi=acos(-1.0);
- const double e=exp();
- const int N = ;
- #define lson i << 1,l,m
- #define rson i << 1 | 1,m + 1,r
- map<LL,LL> mp;
- char con[N];
- int main()
- {
- LL i,p,j,n,check;
- LL cont=,ans=,len1,len2;
- scanf("%lld",&n);
- getchar();
- for(j=;j<=n;j++)
- {
- p=check=;
- len1=len2=;
- memset(con,,sizeof());
- scanf("%s",con);
- for(i=;i<;i++)
- {
- if(con[i]==)
- break;
- if(con[i]=='(')
- {
- len1++;
- p++;
- }
- else
- {
- p--;
- if(len1)
- len1--;
- else
- len2++;
- }
- }
- if(len1==&&len2==)
- cont++;
- else
- {
- if(len1==)
- mp[p]++;
- if(len2==)
- mp[p]++;
- }
- }
- ans=cont*cont;
- map<LL,LL>::iterator it1;
- for(it1=mp.begin();it1!=mp.end();it1++)
- {
- if(it1->first>)
- break;
- if(mp[-(it1->first)]>)
- ans+=(it1->second)*mp[-(it1->first)];
- }
- printf("%lld\n",ans);
- return ;
- }
CodeForces 990C的更多相关文章
- Bracket Sequences Concatenation Problem CodeForces - 990C(括号匹配水题)
明确一下 一个字符串有x左括号不匹配 和 另一个字符串有x个右括号不匹配 这俩是一定能够匹配的 脑子有点迷 emm... 所以统计就好了 统计x个左括号的有几个,x个右括号的有几个 然后 乘一 ...
- Codeforces 990C (模拟+组合数学)
题面: 传送门 分析: 此题O(n2l)" role="presentation" style="position: relative;">O( ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- Java String简单知识点总结
1.字符串的比较 public void run(){ //str1在池中 String str1 = new String("String"); //str2,str3 存在于堆 ...
- web移动端
h5:低版本(IE8及以下不支持H5标签,要引入html5shiv.js才能正常运行) 条件引入,只是针对PC端,移动端不存在这样的操作 <figure>:专门用来存放图片和相关介绍的 & ...
- 小程序获取access_token
<?php //小程序appid $appid = 'wx79d7c348d19f010c'; //小程序 APPSecret 密钥 $appsecret = 'd624aca86d0350ee ...
- 【Linux 命令】- tar 命令
语法 tar [-ABcdgGhiklmMoOpPrRsStuUvwWxzZ][-b <区块数目>][-C <目的目录>][-f <备份文件>][-F <Sc ...
- JavaScript 保留两位小数
以下我们将为大家介绍 JavaScript 保留两位小数的实现方法: 四舍五入 以下处理结果会四舍五入: var num =2.446242342; num = num.toFixed(2); // ...
- Graph-Based image segmentation method
1.Graph-Based 方法简介 基于图的图像分割方法将图像伪造成带权值无向图的形式 : G = (V, E) 其中,V是顶点集合,把图像中的每个像素或者每个区域看成图的一个顶点:E是边的集合,连 ...
- 洛谷P3938 斐波那契
题目戳 题目描述 小 C 养了一些很可爱的兔子. 有一天,小 C 突然发现兔子们都是严格按照伟大的数学家斐波那契提出的模型来进行 繁衍:一对兔子从出生后第二个月起,每个月刚开始的时候都会产下一对小兔子 ...
- NOI2018前的每日记录
NOI2018前的每日记录 开头 今天是\(2018.7.2\),不知不觉已经这么久了.本来还是高一的小蒟蒻,过不了多久就要成为高二的老年选手了. 再过半个月我也要去\(NOI\)打酱油了.我这种D类 ...
- 洛谷 P2530 [SHOI2001]化工厂装箱员 解题报告
P2530 [SHOI2001]化工厂装箱员 题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B ...
- CVPR 2014 ObjectnessBING 原文翻译
BING: Binarized Normed Gradients for Objectness Estimation at 300fps Ming-Ming Cheng, Ziming Zhang, ...