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 ...
随机推荐
- C# 日志监控软件 基于 FileSystemWatcher 02
基于上一篇<C# 日志监控软件 基于 FileSystemWatcher>改进 using System; using System.Collections.Generic; using ...
- 脱离于ASP.NET 和Visual Studio编辑Razor脚本
Razor Pad是一个编辑Razor脚本的工具,脱离于ASP.NET 和Visual Studio. github地址:https://github.com/RazorPad/RazorPad 如果 ...
- KingbaseES V8R6 表空间加密
透明存储加密优势 透明存储加密可确保加密敏感数据满足合规性要求,并提供简化加密操作的功能,优势如下: 作为安全管理员,您可以确保敏感数据已加密,因此在存储介质或数据文件被盗或入侵者试图从操作系统访问数 ...
- Scala编译原理
1 package com.atguigu.chapter01; 2 /** 3 * 4 */ 5 //main 方法名 6 //小括号表示参数列表 7 // 参数声明方式: java -> 类 ...
- #Multi-SG#HDU 3032 Nim or not Nim?
题目 有\(n\)堆石子,每次可以从一堆中取出若干个或是将一堆分成两堆非空的石子, 取完最后一颗石子获胜,问先手是否必胜 分析 它的后继还包含了分成两堆非空石子的SG函数,找规律可以发现 \[SG[x ...
- #dp#洛谷 4158 [SCOI2009]粉刷匠
题目 分析 首先每条木板可以分开处理,再合并起来求最大值 下面讲一下单独处理每条木板的情况 设\(dp[k][m]\)表示前\(m\)个格子粉刷了\(k\)次最多正确粉刷的格子数 那么 \[dp[k] ...
- OpenHarmony社区运营报告(2023年12月)
• 截至2023年12月22日,OpenAtom OpenHarmony(简称"OpenHarmony")社区累计超过6700名贡献者,产生26.9万多个PR,2.4万多个St ...
- 配置java.library.path加载库文件
前言:本文将告诉你Java加载dll或so库文件,配置 java.library.path路径 规避异常:java.lang.UnsatisfiedLinkError: no XXX in java. ...
- VS2019 开发 MFC ACtivex (OCX)控件
需求: js调用ocx方法,传递字符串到ocx控件中显示 操作步骤: 一.新建 ocx 项目 二.填写项目信息 三.完成项目创建 四.修改项目属性 打开 项目属性 -> 链接器 -> ...
- 知识图谱增强的KG-RAG框架
昨天我们聊到KG在RAG中如何发挥作用,今天我们来看一个具体的例子. 我们找到一篇论文: https://arxiv.org/abs/2311.17330 ,论文的研究人员开发了一种名为知识图谱增强的 ...