ACM준비/algospot

FIXPAREN

조규현15 2015. 9. 10. 01:23
반응형

https://algospot.com/judge/problem/read/FIXPAREN

 

algospot.com :: FIXPAREN

Mismatched Parenthesis 문제 정보 문제 You are working on a sequence of parentheses. There are four types of parentheses and the symbols used are ( ), { }, [ ], < >. We call '(', '{', '[', and '<' the left parentheses and ')', '}', ']', and '>' the ri

algospot.com

stack을 사용하여 풀 수 있었다.

#include 
#include 

using namespace std;

char STR1[120];
char STR2[4];
char STR3[4];

char ReturnChar(char c)
{
	if (c == '(')
		return ')';
	else if (c == '{')
		return '}';
	else if (c == '[')
		return ']';
	else if (c == '<')
		return '>';
}
int Winner(char c)
{
	for (int i = 0; i < 4; i++)
	if (STR2[i] == c || STR3[i] == c)
		return i;
}
void Calculate(char &a, char&b)
{
	if (Winner(a) < Winner(b))
		b = STR3[Winner(a)];
	else if (Winner(a) > Winner(b))
		a = STR2[Winner(b)];
}
bool isLeft(char c)
{
	if (c == '('
		|| c == '{'
		|| c == '['
		|| c == '<')//LEFT
		return true;
	else//RIGHT
		return false;
}
int main()
{
	int T;
	for (scanf("%d", &T); T > 0; T--)
	{
		scanf("%s %s", &STR1, &STR2);

		for (int i = 0; i < 4; i++)
			STR3[i] = ReturnChar(STR2[i]);

		stack S;

		for (int i = 0; STR1[i] != '\0'; i++)
		{
			if (isLeft(STR1[i]))//LEFT
			{
				S.push(i);
			}
			else//RIGHT
			{
				int I = S.top();
				Calculate(STR1[I], STR1[i]);
				S.pop();
			}
		}
		printf("%s\n", STR1);
	}
	return 0;
}
반응형

'ACM준비 > algospot' 카테고리의 다른 글

IMGFILTER  (0) 2015.09.10
JOSEPHUS  (0) 2015.09.10
POTION  (0) 2015.09.10
COINS  (0) 2015.09.09
INSERTION  (0) 2015.09.08