small_Dongpo @ 2024-03-22 11:12:45
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
typedef long long l;
string s, ans = "";
stack<char> st;
int main() {
cin >> s;
for (l i = 0; i < s.size(); ++i) {
if (s[i] == '(') {
while (!st.empty() && st.top() == '[') {
ans.push_back(']');
st.pop();
}
st.push('(');
ans.push_back('(');
} else if (s[i] == '[') {
while (!st.empty() && st.top() == '(') {
ans.push_back(')');
st.pop();
}
st.push('[');
ans.push_back('[');
} else if (s[i] == ')') {
if (st.top() == '(') {
st.pop();
ans.push_back(')');
} else {
while (!st.empty() && st.top() == '[') {
ans.push_back(']');
st.pop();
}
ans.push_back('(');
ans.push_back(')');
}
} else if (s[i] == ']') {
if (st.top() == '[') {
st.pop();
ans.push_back(']');
} else {
while (!st.empty() && st.top() == '(') {
ans.push_back(')');
st.pop();
}
ans.push_back('[');
ans.push_back(']');
}
}
}
while (!st.empty()) {
if (st.top() == '(') {
ans.push_back(')');
} else {
ans.push_back(']');
}
st.pop();
}
cout << ans;
return 0;
}
by sss12 @ 2024-03-22 12:55:24
@small_Dongpo 给个样例吧 比如
( [ ) ] )
输出的应该是
( [ ( ) ] )
而不是
( ) [ ] ( ) [ ] ( )
by small_Dongpo @ 2024-03-22 21:56:16
现在的代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
typedef long long l;
string s, ans = "";
stack<char> st;
int main() {
cin >> s;
for (l i = 0; i < s.size(); ++i) {
if (s[i] == '(') {
st.push('(');
ans.push_back('(');
} else if (s[i] == '[') {
st.push('[');
ans.push_back('[');
} else if (s[i] == ')') {
if (!st.empty() && st.top() == '(') {
st.pop();
ans.push_back(')');
} else {
ans.push_back('(');
ans.push_back(')');
}
} else if (s[i] == ']') {
if (!st.empty() && st.top() == '[') {
st.pop();
ans.push_back(']');
} else {
ans.push_back('[');
ans.push_back(']');
}
}
}
while (!st.empty()) {
if (st.top() == '(') {
ans.push_back(')');
} else {
ans.push_back(']');
}
st.pop();
}
cout << ans;
return 0;
}
60pts
by tysy_020 @ 2024-07-12 13:07:13
MAN