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. Postman模拟向Eureka注册服务

    1.官方地址:https://github.com/Netflix/eureka/wiki/Eureka-REST-operations 2.自己搭建一个 Eureka服务运行起来. 3.打开Post ...

  2. #分治NTT#CF1218E Product Tuples

    Codeforces 用 OGF 表示 \(F(B,x)\) 就是 \[\prod_{i=1}^n(1+(q-a_i)x) \] 直接分治 NTT 把 \([x^k]\) 也就是这一位的系数求出来就可 ...

  3. JDK14性能管理工具:jstack使用介绍

    目录 简介 jstack的命令格式 jstack的使用 总结 简介 在之前的文章中,我们介绍了JDK14中jstat工具的使用,本文我们再深入探讨一下jstack工具的使用. jstack工具主要用来 ...

  4. Unity中国、Cocos为OpenHarmony游戏生态插上腾飞的翅膀

      2023年是OpenHarmony游戏生态百花齐放的一年!为了扩展OpenHarmony游戏生态,OpenHarmony在基金会成立了游戏SIG小组,游戏SIG小组联合cocos,从cocos2d ...

  5. SQL 的 AND、OR 和 NOT 运算符:条件筛选的高级用法

    AND 运算符 SQL的AND运算符用于根据多个条件筛选记录,确保所有条件都为TRUE才返回记录.下面是AND运算符的基本语法: SELECT column1, column2, ... FROM t ...

  6. SpringBoot使用@Value注入静态属性

    说明:SpringBoot中使用yml文件配置自定义属性,读取配置文件属性注入到实体类中,属性值都为静态属性 配置文件:yml #用户信息 userMag: userName: "王小波&q ...

  7. 应用缺少POI数据,如何开发地点深度信息?

    用户在App里搜索某个地点时,并不满足单一的地点信息,希望得到更多可以帮助其做决策的深度信息.例如有打车出行需求的用户,在打车App里搜索地点时可以显示周边的地点,精确到某个路口,让用户可以自由选择合 ...

  8. 实例讲解昇腾 CANN YOLOV8 和 YOLOV9 适配

    本文分享自华为云社区<昇腾 CANN YOLOV8 和 YOLOV9 适配>,作者:jackwangcumt. 1 概述 华为昇腾 CANN YOLOV8 推理示例 C++样例 , 是基于 ...

  9. mysql 重新整理——索引简介[七]

    前言 百度百科索引: 在关系数据库中,索引是一种单独的.物理的对数据库表中一列或多列的值进行排序的一种存储结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单. ...

  10. 嘉楠k210 kd233官方demo板gpio点灯实验

    使用maixpy  micropython开发 import utime from Maix import GPIO from board import board_info from fpioa_m ...