소감

  • 통과O
  • 예전같으면 일일이 만들었을 함수들을 그냥 string 함수로 해결

소스

~cpp 
//384
#include <iostream>
using namespace std;
#include <string>

bool isSlump(string str)
{
	int length = str.length();
	int temp = -1;
	if (length < 3)
		return false;
	if (str[0] != 'D' && str[0] != 'E')
		return false;
	if (str[1] != 'F')
		return false;
	temp = str.find_first_not_of('F', 2);
	if (temp == -1)
		return false;
	if (temp == length - 1 && str[temp] == 'G')
		return true;
	else
		return isSlump(str.substr(temp, length - temp));
}

bool isSlimp(string str)
{
	int length = str.length();
	if (length < 2)
		return false;
	if (str[0] != 'A')
		return false;
	if (length == 2 && str[1] == 'H')
		return true;
	if (length != 2 && str[length - 1] == 'C')
	{
		if (str[1] == 'B')
			return isSlimp(str.substr(2, length - 3));
		else
			return isSlump(str.substr(1, length - 2));
	}
	return false;
}

bool isSlurpy(string str)
{
	int i, j, temp = -1;	// temp는 Slimp의 마지막 문자 위치
	int length = str.length();
	temp = str.find_last_of('C');
	if (temp == -1)
		temp = str.find_last_of('H');
	
	// Slimp와 Slump로 분리해서 검사
	if (temp != -1 && isSlimp(str.substr(0, temp + 1)) && isSlump(str.substr(temp + 1, length - temp - 1)))
		return true;
	else
		return false;
}

int main()
{
	int numberOfCase;
	cin >> numberOfCase;
	int testCase, i, j;
	cout << "SLURPYS OUTPUT" << endl;
	for (testCase = 0; testCase < numberOfCase; testCase++)
	{
		string str;
		cin >> str;
		if (isSlurpy(str))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	cout << "END OF OUTPUT" << endl;
	return 0;
}

댓글

Retrieved from http://wiki.zeropage.org/wiki.php/Slurpys/곽세환
last modified 2021-02-07 05:28:02