Genshineer @ 2021-12-14 21:10:38
#include <bits/stdc++.h>
using namespace std;
char s[105];
stack<int> s1, s2;
bool b[105];
int main( ) {
memset(b, 0, sizeof(b));
cin >> s;
for(int i = strlen(s) - 1; i >= 0; i--) {
if(s[i] == ']') {
s2.push(i);
// cout << "push2" << endl;
}
if(s[i] == ')') {
s1.push(i);
// cout << "push1" << endl;
}
if(1) {
if(s[i] == '(') {
// cout << "go with(" << endl;
if(!s1.empty() || !s2.empty()){
while(!s1.empty() && !s2.empty() && s2.top() > i && s2.top() < s1.top()) {
s2.pop();
}
if(!s1.empty()) {
b[i] = 1;
b[s1.top()] = 1;
// cout << i << " " << s1.top() << endl;
s1.pop();
}
}
}
if(s[i] == '[') {
// cout << "go with[" << endl;
if(!s1.empty() || !s2.empty()) {
while(!s1.empty() && !s2.empty() && s1.top() > i && s1.top() < s2.top()) {
s1.pop();
}
if(!s2.empty()) {
b[i] = 1;
b[s2.top()] = 1;
// cout << i << " " << s2.top() << endl;
s2.pop();
}
}
}
}
// cout << s[i] << endl;
}
for(int i = 0; i < strlen(s); i++) {
if(b[i] != 1) {
if(s[i] == '(' || s[i] == ')') {
cout << "()";
}
else {
cout << "[]";
}
}
else {
cout << s[i];
}
}
/* cout << endl;
for(int i = 0; i < s.size(); i++) {
cout << b[i] << " ";
}*/
}
by PassName @ 2021-12-15 22:35:04
for(int i = strlen(s) - 1; i >= 0; i--) 这个地方,你把它换成i=0来解就行了(后面的也一样)