P1241 68分求调
  • 板块P1241 括号序列
  • 楼主Duckh
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/26 15:22
  • 上次更新2024/8/26 17:26:34
查看原帖
P1241 68分求调
1064058
Duckh楼主2024/8/26 15:22
#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;
}
2024/8/26 15:22
加载中...