stoneoceam @ 2022-07-08 13:26:54
#include<iostream>
#include<string>
using namespace std;
typedef struct LNode {
string s = "";
int k;//匹配成功为1,不成功为0
}LNode;
int main() {
string s;
cin >> s;
s.insert(0, " ");//0号位置空出
int len = s.size();
int flag = 0;
LNode* kk = new LNode[len];
for (int i = 1; i < len; i++) {
if (s[i] == '(' || s[i] == '[') {
flag = i;//标记为最近的左括号
kk[i].s.push_back(s[i]);
kk[i].k = 0;
}
else {
kk[i].s.push_back(s[i]);
kk[i].k = 0;
if (flag != 0 &&((s[flag] == '(' && s[i] == ')') || (s[flag] == '[' && s[i] == ']'))) {
kk[flag].k = kk[i].k = 1;
flag = NULL;
}
}
}
for (int i = 1; i < len; i++) {
if (kk[i].k == 0) {
if (kk[i].s == "(")
cout << kk[i].s << ")";
if (kk[i].s == "[")
cout << kk[i].s << "]";
if (kk[i].s == ")")
cout << "(" << kk[i].s;
if (kk[i].s == "]")
cout << "[" << kk[i].s;
}
else
cout << kk[i].s;
}
return 0;
}