반응형
https://www.acmicpc.net/problem/1343
문제 해결 알고리즘
X가 연달아 있는 부분에서 X의 개수가 홀수이면 -1을 출력해주고 짝수이면 4개씩 A로 채우다 2개가 남으면 B로 채워준다. '.'은 그대로 출력해준다.
소스 코드
#include <bits/stdc++.h>
using namespace std;
int main(){
string str, result = ""; cin >> str;
string A = "AAAA", B = "BB";
int cnt = 0;
for(int i=0;i<str.size();i++){
if(str[i] != 'X') {
if(cnt % 2 != 0) {
cout << -1;
return 0;
}
for(int j=0;j<cnt/4;j++) result += A;
if(cnt%4==2) result += B;
result = result + '.';
cnt = 0;
}
else cnt++;
}
if(cnt % 2 != 0) {
cout << -1;
return 0;
}
for(int j=0;j<cnt/4;j++) result += A;
if(cnt%4==2) result += B;
cout << result;
}
반응형
'알고리즘 문제 해결 > BOJ' 카테고리의 다른 글
[BFS] BOJ 12852 1로 만들기 2 (0) | 2021.10.22 |
---|---|
[브루트 포스] BOJ 10971 외판원 순회 2 (0) | 2021.10.19 |
[분할 정복] BOJ 2630 색종이 만들기 (0) | 2021.10.08 |
[DP] BOJ 17626 Four Squares (0) | 2021.10.04 |
[DP] BOJ 1699 제곱수의 합 (0) | 2021.10.02 |