Duckh @ 2024-08-26 15:22:36
#include <bits/stdc++.h>
using namespace std;
stack <pair <char, int> > st;
unordered_map <int, bool> mp;
int main()
{
string exp;
cin>>exp;
for (int i = 0; i < exp.length(); i ++)
{
if (exp[i] == '(' || exp[i] == '[')
{
st.push(make_pair(exp[i], i));
}
if (exp[i] == ')')
{
if (st.empty())
{
st.push(make_pair(exp[i], i));
continue;
}
else if (st.top().first == '(')
{
st.pop();
}
else
{
st.push(make_pair(exp[i], i));
}
}
if (exp[i] == ']')
{
if (st.empty())
{
st.push(make_pair(exp[i], i));
continue;
}
else if (st.top().first == '[')
{
st.pop();
}
else
{
st.push(make_pair(exp[i], i));
}
}
}
while (!st.empty())
{
//cout<<st.top().first;
mp[st.top().second] = true;
st.pop();
}
for (int i = 0; i < exp.length(); i ++)
{
if (mp[i] == true)
{
if (exp[i] == '(')
{
cout<<exp[i];
cout<<")";
}
else if (exp[i] == ')')
{
cout<<"(";
cout<<exp[i];
}
else if (exp[i] == '[')
{
cout<<exp[i];
cout<<"]";
}
else if (exp[i] == ']')
{
cout<<"[";
cout<<exp[i];
}
}
else
{
cout<<exp[i];
}
}
return 0;
}