CF990C Bracket Sequences Concatenation Problem 思维 第五道 括号经典处理题目
2 seconds
256 megabytes
standard input
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.
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.
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.
3
)
()
(
2
2
()
()
4
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 思维 第五道 括号经典处理题目的更多相关文章
- CF思维联系– Codeforces-990C Bracket Sequences Concatenation Problem(括号匹配+模拟)
ACM思维题训练集合 A bracket sequence is a string containing only characters "(" and ")" ...
- Bracket Sequences Concatenation Problem括号序列拼接问题(栈+map+思维)
A bracket(括号) sequence is a string containing only characters "(" and ")".A regu ...
- Bracket Sequences Concatenation Problem CodeForces - 990C(括号匹配水题)
明确一下 一个字符串有x左括号不匹配 和 另一个字符串有x个右括号不匹配 这俩是一定能够匹配的 脑子有点迷 emm... 所以统计就好了 统计x个左括号的有几个,x个右括号的有几个 然后 乘一 ...
- CF 990C. Bracket Sequences Concatenation Problem【栈/括号匹配】
[链接]:CF [题意]: 给出n个字符串,保证只包含'('和')',求从中取2个字符串链接后形成正确的括号序列的方案数(每个串都可以重复使用)(像'()()'和'(())'这样的都是合法的,像')( ...
- 再来五道剑指offer题目
再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
- D. Yet Another Subarray Problem 思维 难 dp更好理解
D. Yet Another Subarray Problem 这个题目很难,我比赛没有想出来,赛后又看了很久别人的代码才理解. 这个题目他们差不多是用一个滑动窗口同时枚举左端点和右端点,具体如下: ...
- 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 ...
- cf1088D. Ehab and another another xor problem(思维)
题意 题目链接 系统中有两个数\((a, b)\),请使用\(62\)以内次询问来确定出\((a, b)\) 每次可以询问两个数\((c, d)\) 若\(a \oplus c > b \opl ...
- 【cf1272】F. Two Bracket Sequences
传送门 题意: 给出\(s,t\)两个合法括号序列,现在找到一个长度最小的合法的序列\(p\),使得\(s,t\)都为其子序列. 思路: 考虑\(dp:dp[i][j][d]\)表示第一个串在\(i\ ...
随机推荐
- 基于TP5.1实用案例及教程
推荐<基于TP5.1实用案例及教程>书 目录: 通用封装 Export通用封装Import通用封装配合Import通用封装的ImportBaseVerify类Files通用封装Direct ...
- Extjs的使用总结笔记
一:Extjs自带验证 1.alpha //只能输入字母,无法输入其他(如数字,特殊符号等) 2.alphanum//只能输入字母和数字,无法输入其他 3.email//email验证,要求的格式是& ...
- [Inno Setup]写入注册表时32位系统和64位系统的路由
昨天下午组内一位同事跟说,他想在Inno Setup的安装包中写入一个注册表.目标位置是HKLM:\Software\下面创建自己的注册表项.然后说尝试了好几次都不行, 但是往HKCU下面写入却是OK ...
- Java虚拟机学习笔记(二)--- 判断对象是否存活
Java堆中存放着所有的对象实例,垃圾收集器在堆进行回收之前,需要判断对象是“存活”还是“死亡”(即不可能再被任何途径引用的对象). 最常见的一种判断对象是否存活算法是引用计数算法, 给对象加一个引用 ...
- Mermaid
graph TD; A-->B; A-->C; B-->D; C-->D;
- HTML发展历程
HTML是超文本标记语言的缩写,不同于C或JAVA等编程语言,HTML由标签组成.通过标签可以在网页中插入文字.图片.链接.音频.视频等元素,进而描述网页.和Windows一样,随着技术的发展,HTM ...
- Java学习|强引用,软引用,弱引用,幻想引用有什么区别?
在Java语言中,除了基本数据类型外,其他的都是指向各类对象的对象引用:Java中根据其生命周期的长短,将引用分为4类. 1 强引用 特点:我们平常典型编码Object obj = new Objec ...
- (三十二)c#Winform自定义控件-表格
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- AutoCAD .NET: 遍历模型空间
原文:http://spiderinnet1.typepad.com/blog/2012/06/autocad-net-iterate-through-model-space.html https:/ ...
- win10文件备份、文件同步方案
用个人版onedrive同步重要数据,数据安全有保障,但免费版只有15G空间,需要合理分配.(201907与别人合租家庭版,空间1T充足) google-drive可以同步指定的文件夹,但空间也只有1 ...