题目传送门
c++代码
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
//用数组模拟栈
char stk[N], tt; //内容是第几个
//原字符串
string s;
int b[N];
int main() {
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(' || s[i] == '[') stk[++tt] = i;
else {
if (s[i] == ')' && s[stk[tt]] == '(') b[i] = b[stk[tt]] = 1,tt--;
else if (s[i] == ']' && s[stk[tt]] == '[') b[i] = b[stk[tt]] = 1,tt--;
}
}
//输出
for (int i = 0; i < s.size(); i++) {
if (b[i]) cout << s[i];
else if (s[i] == '(' || s[i] == ')') cout << "()";
else if (s[i] == '[' || s[i] == ']') cout << "[]";
}
return 0;
}