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. layui select onchange事件处理

    1.html代码如下,不要忘记 lay-filter属性 <div class="layui-input-block">         <select id=& ...

  2. IDEA MyBatis Log 插件,打印SQL语句

    打开Settings->plugins 搜索插件 MyBatis Log点击安装,完成后重启IDEA即可. 点击Tools,选择 MyBatis Log Plugin ,会在下方打开一个窗口,这 ...

  3. 不到2000字,轻松带你搞懂STM32中GPIO的8种工作模式

    大家好,我是知微! 学习过单片机的小伙伴对GPIO肯定不陌生,GPIO (general purpose input output)是通用输入输出端口的简称,通俗来讲就是单片机上的引脚. 在STM32 ...

  4. 初识urllib与requests

    urllib与requests 一.urllib的学习 学习目标 了解urllib的基本使用 1.urllib介绍 除了requests模块可以发送请求之外, urllib模块也可以实现请求的发送,只 ...

  5. #前缀和,后缀和#洛谷 4280 [AHOI2008]逆序对

    题目传送门 分析 首先填的数字单调不降,感性理解 那可以维护\([a_1\sim a_{i-1}]\)的\(cnt\)后缀和以及 \([a_{i+1}\sim a_n]\)的\(cnt\)前缀和,那可 ...

  6. C 多维数组、特殊字符和字符串函数详解

    C 多维数组 数组,也称为单维数组.这些非常棒,是您在 C 语言编程中会经常使用的东西.然而,如果您想要将数据存储为表格形式,例如带有行和列的表格,则需要熟悉多维数组. 二维数组 二维数组也称为矩阵, ...

  7. HMS Core Discovery第13期直播预告——构建手游中的真实世界

    [导读] 游戏的迭代升级不止在于玩法的创新,也体现在画质升级上.一款又一款次世代游戏运用各种顶尖渲染技术化身"显卡杀手"的同时,也让玩家们在体验过逼真渲染画质后大呼过瘾,技术的进步 ...

  8. Qt线程简单使用三:QRunnable~线程池

    需求: 点击QPushButton按钮,QLabel中的数字,不断累加,一直到999.   做法: 创建任务类,点击QPushButton后,将任务类放入线程池中运行,通过任务类while循环,不断发 ...

  9. Unity 音频资源优化

    1.声道设置 (1).不设置 单声道 音频大小为下图 (2).设置单声道 音频大小为下图 2.加载类型 (1).Decompress On Load 使用内存8.1M (2).Compressed I ...

  10. HarmonyOS数据管理与应用数据持久化(二)

    通过键值型数据库实现数据持久化 场景介绍 键值型数据库存储键值对形式的数据,当需要存储的数据没有复杂的关系模型,比如存储商品名称及对应价格.员工工号及今日是否已出勤等,由于数据复杂度低,更容易兼容不同 ...