백준 1343 c++

magicdrill·2024년 3월 1일
0

백준 문제풀이

목록 보기
69/654

백준 1343 c++

사실 어려운 문제는 아닌데 생각을 너무 많이 하다가 풀이가 오래 걸렸다.
단순하게 생각한다고 풀은 것도 조건을 너무 많이 붙이고 이중반복문도 들어가서 규모가 커지면 성능이 떨어질 것으로 예상된다.
나중에 다시 풀어본다.

#include <iostream>

using namespace std;

string input_board()
{
	string str;

	cin >> str;

	return str;
}

void find_answer(string board)
{
	int length = board.length(), temp_length = 0;
	int i, j;
	string result = "";
	
	for (i = 0; i < length; i++)
	{	
		if (board[i] == 'X')
		{
			temp_length++;
			if (board[i + 1] == '.' || i + 1 == length)
			{
				if (temp_length % 2 == 1)
				{
					cout << "-1\n";
					return;
				}
				if (temp_length == 2)
				{
					result += "BB";
				}
				else if (temp_length == 4)
				{
					result += "AAAA";
				}
				else//temp_length > 4
				{
					for (j = 0; j < temp_length / 4; j++)
					{
						result += "AAAA";
					}
					if (temp_length % 4 == 2)
					{
						result += "BB";
					}
				}
				temp_length = 0;
			}
		}
		else
		{
			result += '.';
		}
	}
	cout << result << "\n";

	return;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	string board;

	board = input_board();
	find_answer(board);

	return 0;
}

0개의 댓글