我是靠谱客的博主 高贵雨,这篇文章主要介绍HDU5831 Rikka with Parenthesis II 2016多校第八场11HDU5831 Rikka with Parenthesis II (2016多校第八场11),现在分享给大家,希望可以做个参考。

HDU5831 Rikka with Parenthesis II (2016多校第八场11)

题目

大意:给出一串括号,问对换一对后能否匹配

题解

第一个)和最后一个(交换,然后用栈验证
注意点:
1. ))((这种改变一对影响两对,比赛的时候我们队就卡了一下这里
2. 有正确的交换完变成错误的,例如()

代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream> #include <string> #include <algorithm> #include <iomanip> using namespace std; int main() { ios::sync_with_stdio(false); int s[100001], n, top, i, j, T, left1, right0; char t[100001]; cin>>T; while(T--) { cin>>n; cin>>t; top = -1; left1 = -1; right0 = n; for(i = 0; i < n; ++i) { if(left1 == -1 && t[i] == ')') left1 = i; if(t[i] == '(') right0 = i; } swap(t[left1], t[right0]); for(i = 0; i < n; ++i) { if(t[i] == '(') s[++top] = 0; else if(top >= 0 && s[top] == 0) --top; else s[++top] = 1; } if(top == -1) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }

最后

以上就是高贵雨最近收集整理的关于HDU5831 Rikka with Parenthesis II 2016多校第八场11HDU5831 Rikka with Parenthesis II (2016多校第八场11)的全部内容,更多相关HDU5831内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部