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的更多相关文章

  1. C Yuhao and a Parenthesis

    Yuhao and a Parenthesis time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. Hello 2019题解

    Hello 2019题解 题解 CF1097A [Gennady and a Card Game] map大法好qwq 枚举每一个的第\(1,2\)位判是否与给定的重复即可 # include < ...

  3. codeforces 1097 Hello 2019

    又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std; cha ...

  4. Hello 2019 Solution

    A. Gennady and a Card Game 签到. #include <bits/stdc++.h> using namespace std; ], t[]; bool solv ...

  5. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  6. 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis

    1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...

  7. 2016年省赛G题, Parenthesis

    Problem G: Parenthesis Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 398  Solved: 75[Submit][Status ...

  8. HDU 5831 Rikka with Parenthesis II(六花与括号II)

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  9. CSU 1809 Parenthesis(线段树+前缀和)

    Parenthesis Problem Description: Bobo has a balanced parenthesis sequence P=p1 p2-pn of length n and ...

  10. HDU 5831 Rikka with Parenthesis II (栈+模拟)

    Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

随机推荐

  1. 用于vivado的TCL脚本创建工程

    用于实现在脚本所在的目录创建工程并初始化工程 点击查看代码 #获取脚本所在的文件 variable My_File [file normalize [info script]] #prj_path_f ...

  2. quartus之rom的IP测试

    quartus之rom的IP测试 1.rom的作用 rom,就是只读存储器,内部数据在下载电路时就已经确认,不能使用信号驱动更改,只能够读取,一般用于比较重要的配置数据.在quartus中,可以直接调 ...

  3. KingbaseES V8R6 集群运维案例 -- 集群断电重新加电后恢复

    ​ 官方文档介绍: https://help.kingbase.com.cn/v8/highly/availability/cluster-use/cluster-use-2.html#id35 全局 ...

  4. Visual Studio快捷键总览,推荐VS+Resharper实现高效开发

    VS2022之后,其实还挺好用的,但个人还是习惯VS+Resharper的强强组合,尤其是Ctrl+N快捷键的全局搜,比VS自带的Ctrl+T好用太多了,Ctrl+B还能直接查看反编译之后的dll的方 ...

  5. 技术文档指南:版本说明、网站文案、FAQ、案例研究与内容优化

    Release Notes 和产品公告 Release Notes 通常是软件文档的一部分,是在新产品发布时提供给用户的简短.高级摘要.它们包含有关更新的重要信息,包括新功能.增强功能.错误修复,通常 ...

  6. QImage:使用QImage构造函数加载图像和使用成员函数loadFromData加载图像的区别

    结论: QImage构造函数,既可以加载内存图像数据,也可以加载二进制文件数据 loadFromData成员函数,只能加载二进制文件数据 loadFromData Qt帮助文档说明 编写测试代码验证 ...

  7. vue-cli4,vue3打包后页面无内容

    这个问题百度了一下,各种各样的的回答都有,试了好多种方法,终于解决这个问题 解决方法: 1.在项目根目录下,新建  vue.config.js, 在文件中输入: module.exports = { ...

  8. 详解Java Chassis 3与Spring Cloud的互操作

    本文分享自华为云社区<Java Chassis 3技术解密:与Spring Cloud的互操作>,作者: liubao68. Java Chassis 3一个很重要的设计原则:利用架构的韧 ...

  9. openGauss资源池化开发者入门指南(一)

    openGauss资源池化开发者入门指南(一) 一.内容简介 openGauss 资源池化是 openGauss 推出的一种新型的集群架构.通过 DMS 和 DSS 组件,实现集群中多个节点的底层存储 ...

  10. 从 Oracle 到 MySQL 数据库的迁移之旅

    目录 引言 一.前期准备工作 1.搭建新的MySQL数据库 2 .建立相应的数据表 2.1 数据库兼容性分析 2.1.1 字段类型兼容性分析 2.1.2 函数兼容性分析 2.1.3 是否使用存储过程? ...