GitHubSeob

C++ / 백준 / 1343 / 폴리오미노 본문

Baekjoon/Silver

C++ / 백준 / 1343 / 폴리오미노

GitHubSeob 2023. 7. 4.
문제

 

https://www.acmicpc.net/problem/1343

 

1343번: 폴리오미노

첫째 줄에 사전순으로 가장 앞서는 답을 출력한다. 만약 덮을 수 없으면 -1을 출력한다.

www.acmicpc.net

문제풀이

 

istringstream을 이용해 풀었다.

입력을 받으면 EOF까지 탐색하면서 구분자인 '.'을 기준으로 문자열을 자른다.

'.'을 기준으로 나눴을 때 X가 홀수개라면 -1을 바로 출력한다.

 

그 외에는 X만큼 fill을 이용해 B를 먼저 채우고, 처음부터 4의 배수개까지 A로 채운다.

답에는 '.'도 포함해야 하므로 answer의 크기와 입력받은 input의 크기가 다르면 answer에 '.'을 더했다.

코드
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <string>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	string board(""), block(""), answer("");
	cin >> board;

	istringstream istr(board);

	while (getline(istr, block, '.')) {
		if (block.size() % 2 == 1) {
			cout << -1;
			return 0;
		}
		else {
			fill(block.begin(), block.end(), 'B');
			fill(block.begin(), block.begin() + block.size() / 4 * 4, 'A');
			answer += block;
		}
		if (answer.size() != board.size()) {
			answer += '.';
		}
	}
	cout << answer;
}

 

'Baekjoon > Silver' 카테고리의 다른 글

C++ / 백준 / 13305 / 주유소  (0) 2023.07.04
C++ / 백준 / 2217 / 로프  (2) 2023.07.04
C++ / 백준 / 14916 / 거스름돈  (0) 2023.07.04
C++ / 백준 / 2529 / 부등호  (0) 2023.06.11
C++ / 백준 / 1302 / 베스트셀러  (0) 2022.03.31