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. [HTML、CSS]知识点

    [版权声明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) https://blog.csdn.net/m0_69908381/article/details/130176402 出自[进步* ...

  2. SpringBoot项目 填充Excel模板 导出 下载

    模板 下载后的效果 项目结构 pom <?xml version="1.0" encoding="UTF-8"?> <project xmln ...

  3. KingbaseES查找慢查询语句和阻塞会话

    在处理数据库性能问题时,识别和分析慢查询及阻塞会话是至关重要的步骤.数据库管理员和开发人员常常需要依赖特定的工具和查询语句来追踪这些性能瓶颈. 当数据库响应变慢或出现处理延迟时,第一步通常是查找那些执 ...

  4. C++ 简单实现shared_ptr

    共享指针 管理指针的存储,提供有限的垃圾回收工具,并可能与其他对象共享该管理. shared_ptr类型的对象都能够获得指针的所有权并共享该所有权:一旦它们获得所有权,当最后一个所有者释放该所有权时, ...

  5. #动态规划#CF889E Mod Mod Mod

    题目传送门 分析 这道题有一个很妙的地方就是将一段前缀整体一起做. 设 \(dp[i][j]\) 表示\(x\) 被前 \(i\) 个数取模后答案最大,并且 \(j\) 为取得此答案的最大值 最后再对 ...

  6. #凸包,闵可夫斯基和#CF87E Mogohu-Rea Idol

    题目 按逆时针顺序给出三个凸包点集 \(\mathbb{A,B,C}\),每次查询给出点 \(D\), 问是否存在点 \(A\in\mathbb{A},B\in\mathbb{B},C\in\math ...

  7. 基于EtherNet/IP实现欧姆龙NX系列PLC通信

    1.引言 工业以太网协议 (Ethernet/IP) 是由ODVA所开发并得到了罗克韦尔自动化的强大支持.它使用已用于ControlNet和DeviceNet的控制和信息协议 (CIP) 为应用层协议 ...

  8. 如何跑各种check

    如何进行 Fastcheck? 首先,导入环境变量: export CODE_BASE=/data/openGauss-server export BINARYLIBS=/data/openGauss ...

  9. npm,registry,镜像源,npm切换源,yarn,cnpm,taobao,nrs

    描述 我们在使用 node 的 npm 下载依赖的时候,往往下载速度很慢,那是因为 npm 默认的是 npm 处于国外的官方镜像源.所以需要切换到国内的镜像源来加速依赖下载.所以本文推荐一款简单好用 ...

  10. c# 优化代码的一些规则——什么情况下应该使用new[七]

    前言 new 在重构这本书中写道new就是坏代码的味道,说明使用new的情况并不多. 在这里我指的new 是方法修饰符,而不是指实例. 正文 看下new的作用: new 修饰符可以重新定义从基类继承下 ...