반응형

문제 링크: https://www.acmicpc.net/problem/1343

 

1343번: 폴리오미노

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

www.acmicpc.net

<문제 풀이> 그리디 알고리즘

 

"XXXX"를 찾아서 모두 AAAA로 바꿔주고 그다음 "XX"를 찾아서 모두 "BB"로 바꿔주면 됩니다.

 

 

<C++소스코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<string>
using namespace std;
 
string s;
 
int main() {
    ios_base::sync_with_stdio(false);
 
    cin >> s;
    while (s.find("XXXX"!= string::npos) {
        s.replace(s.find("XXXX"),4 ,"AAAA");
    }
    while (s.find("XX"!= string::npos) {
        s.replace(s.find("XX"),2"BB");
    }
    if (s.find("X"!= string::npos) cout << -1;
    else cout << s;
 
    return 0;
}
cs
 

 

반응형

'알고리즘 문제풀이 > 백준' 카테고리의 다른 글

[백준 20365번] 블로그2  (0) 2023.04.23
[백준 20115번] 에너지 드링크  (0) 2023.04.22
[백준 15489번] 파스칼 삼각형  (0) 2023.04.21
[백준 16953번] A -> B  (0) 2023.04.21
[백준 11508번] 2 + 1 세일  (0) 2023.04.20

+ Recent posts