CF1097C Yuhao and a Parenthesis
CF1097C Yuhao and a Parenthesis
stl 乱搞做法,感觉比正解更直接。
- 每个字符串内部能匹配的尽可能匹配。
- 匹配完成后,检验剩余序列是否只含有
(或只含有)或为空,如果符合条件,插入待选序列。 - 在待选序列中尽可能匹配。
code
- 用一个
set<pair<string, int> >维护新字符串和位置两个信息。 - 记录一个
vis表示当前位置有没有用过。 - 依次遍历每个新字符串 \(s\),查询 \(s\) 的翻转是否在
set中存在。
((((是))))的翻转,不是)))或(的翻转。
具体实现可以利用 pair 的双关键字排序查询 lower_bound(rev(s), -1)
#include<bits/stdc++.h>
using namespace std;
char stk[500000];
int tt;
bool check(string s) {
int t = 0;
for(char c : s) if(c == '(') ++ t;
return (t == (int)s.size()) || t == 0;
}
string get_rev(string s) {
string t = s;
reverse(t.begin(), t.end());
for(char &c : t) c == '(' ? (c = ')') : (c = '(');
return t;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int n;
cin >> n;
vector<string> ss(n);
set<pair<string, int> > se;
vector<bool> vis(n, 0);
for(int i = 0; i < n; ++ i) {
cin >> ss[i];
tt = -1;
for(auto c : ss[i]) {
if(~ tt && stk[tt] == '(' && c == ')') -- tt;
else stk[++ tt] = c;
}
ss[i].clear();
for(int j = 0; j <= tt; ++ j) ss[i] += stk[j];
if(check(ss[i])) se.emplace(ss[i], i);
else vis[i] = 1;
}
int ans = 0;
for(int i = 0; i < n; ++ i) {
if(vis[i]) continue;
se.erase(se.find({ss[i], i}));
vis[i] = 1;
string t = get_rev(ss[i]);
auto it = se.lower_bound({t, -1});
if(it != se.end()) {
auto [rev, j] = *it;
if(rev == t) {
vis[j] = 1;
++ ans;
se.erase({ss[j], j});
}
}
}
cout << ans << '\n';
return 0;
}
CF1097C Yuhao and a Parenthesis的更多相关文章
- C Yuhao and a Parenthesis
Yuhao and a Parenthesis time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Hello 2019题解
Hello 2019题解 题解 CF1097A [Gennady and a Card Game] map大法好qwq 枚举每一个的第\(1,2\)位判是否与给定的重复即可 # include < ...
- codeforces 1097 Hello 2019
又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std; cha ...
- Hello 2019 Solution
A. Gennady and a Card Game 签到. #include <bits/stdc++.h> using namespace std; ], t[]; bool solv ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis
1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...
- 2016年省赛G题, Parenthesis
Problem G: Parenthesis Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 398 Solved: 75[Submit][Status ...
- HDU 5831 Rikka with Parenthesis II(六花与括号II)
31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- CSU 1809 Parenthesis(线段树+前缀和)
Parenthesis Problem Description: Bobo has a balanced parenthesis sequence P=p1 p2-pn of length n and ...
- HDU 5831 Rikka with Parenthesis II (栈+模拟)
Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
随机推荐
- verilog中的数据类型
Verilog中的数据格式 1.基本概念 verilog中写一个数据的通用格式是 n'b000_000_···_000,表示一个n位的二进制数.基于这个通用式,可以将其分为三个部分:位数.加权数和实际 ...
- KingbaseES V8R3 备份恢复案例 -- sys_rman物理备份异机恢复
案例说明: 在生产环境通过sys_rman执行了物理备份后,需要在异机构建测试环境,本案例描述了通过物理备份异机恢复的详细过程及操作. 适用版本: KingbaseES V8R3 节点信息: [kin ...
- Hybrid-PSC:基于对比学习的混合网络,解决长尾图片分类 | CVPR 2021
论文提出新颖的混合网络用于解决长尾图片分类问题,该网络由用于图像特征学习的对比学习分支和用于分类器学习的交叉熵分支组成,在训练过程逐步将训练权重调整至分类器学习,达到更好的特征得出更好的分类器的思想 ...
- archlinux开机出现错误Dependency failed for /home. Dependency failed for Local File System Time outwaiting for device /dev/disk/...
错误如下 Dependency failed for /home. Dependency failed for Local File System Time outwaiting for device ...
- ABC327 A-F
vp on 2023.11.12 A: 使用 string 的 find,find 在找不到时会返回string::npos. B.C:略. D 发现原题相当于二分图判定. E 线性DP. F 原题相 ...
- Spring框架之控制反转IoC(Inversion of Control)的理解
简单理解: 控制反转就是将代码的调用权(控制权)从调用方转移给被调用方(服务提供方). 解释一下: 如果我们需要创建某个类,就需要程序员去修改代码,然后才可以得到想要的类.反转的意思就是不需要程序员去 ...
- #随机#CF1198F GCD Groups 2
题目 将 \(n\) 个数分为两组,使得两组的GCD都为1,求具体的分组情况 分析 考虑直接打乱 \(n\) 个数,如果能使第一组GCD减小就减小,否则丢到第二组, 由于打乱后出错的概率会减小,所以r ...
- jcmd:JDK14中的调试神器
目录 简介 jcmd的语法 列出运行的JVM 打印stack信息 打印heap info 打印heap dump 统计heap使用情况 JFR功能 总结 简介 jcmd是JDK自带的调试工具,具有非常 ...
- Numpy的数组对象
数组对象 NumPy 最重要的一个特点是其 N 维数组对象 ndarray,它是一系列同类型数据的集合,从0 开始进行集合中元素的索引:ndarray 对象是用于存放同类型元素的多维数组,其中的每个元 ...
- C# Dictionary(数据字典)的基本用法
C# Dictionary(数据字典)的基本用法 - Mr.Cat~ - 博客园 (cnblogs.com) 通常情况下,我们可以通过 int 类型的索引来从数组或者 List 集合中查询所需的数 ...