코딩공부/Softeer

[Softeer/C++] [한양대 HCPC 2023] Yeah, but How?

usingsystem 2024. 12. 26. 09:49
728x90

https://softeer.ai/practice/9498

 

Softeer - 현대자동차그룹 SW인재확보플랫폼

 

softeer.ai

소스코드

#include <iostream>
#include <stack>
using namespace std;

int main() {
	string s;
	cin >> s;
	stack<char> stackStr;
	string answer;
	for (auto item : s)
	{
		if (item == '(')
		{
			if (stackStr.empty() == false && stackStr.top() == ')')
			{
				answer += "+(";
			}
			else
			{
				answer += "(";
			}
			stackStr.push(item);
		}
		else
		{
			if (stackStr.empty() == false && stackStr.top() == '(')
			{
				stackStr.pop();
				answer += "1)";
			}
			else
			{
				answer += ")";
			}

			stackStr.push(item);
		}
	}

	cout << answer;

	return 0;
}
728x90