0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-10 04:58:48 +00:00

P1241 括号序列

R43144924
This commit is contained in:
Baoshuo Ren 2020-12-04 08:57:24 +08:00 committed by Baoshuo Ren
parent 0ee0a08c04
commit b37c8f2460
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

47
problem/P1241/P1241.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <bits/stdc++.h>
using namespace std;
char b[105];
int n;
string s;
stack<int> st;
int main() {
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') {
st.push(i);
b[i] = ')';
}
if (s[i] == '[') {
st.push(i);
b[i] = ']';
}
if (s[i] == ')' || s[i] == ']') {
if (st.empty() || b[st.top()] != s[i]) {
if (s[i] == ')') {
b[i] = '(';
}
else {
b[i] = '[';
}
}
else {
b[st.top()] = ' ';
st.pop();
}
}
}
for (int i = 0; i < s.size(); i++) {
if (b[i] == '(' || b[i] == '[') {
cout << b[i];
}
cout << s[i];
if (b[i] == ')' || b[i] == ']') {
cout << b[i];
}
}
cout << endl;
return 0;
}