๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Gold

[BOJ] 2661: ์ข‹์€ ์ˆ˜์—ด

by Jaeguk 2022. 1. 19.
๋ฌธ์ œ ๋งํฌ

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

 

2661๋ฒˆ: ์ข‹์€์ˆ˜์—ด

์ฒซ ๋ฒˆ์งธ ์ค„์— 1, 2, 3์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ๊ธธ์ด๊ฐ€ N์ธ ์ข‹์€ ์ˆ˜์—ด๋“ค ์ค‘์—์„œ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜์—ด๋งŒ ์ถœ๋ ฅํ•œ๋‹ค. ์ˆ˜์—ด์„ ์ด๋ฃจ๋Š” 1, 2, 3๋“ค ์‚ฌ์ด์—๋Š” ๋นˆ์นธ์„ ๋‘์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

ํ’€์ด

์˜ˆ์ „์— ๋ˆ„๊ตฐ๊ฐ€์˜ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ์น˜๋ฉด์„œ ๋Œ€์ถฉ ์ดํ•ด๋งŒ ํ•˜๊ณ  ๋„˜์–ด๊ฐ”๋˜ ๋ฌธ์ œ๋‹ค. ์ด๋ฒˆ์— ๋‹ค์‹œ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ํ’€์–ด๋ณด๋ฉด์„œ

์ œ๋Œ€๋กœ ํ’€์–ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ string์— ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๋ถ™์—ฌ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

์ฒ˜์Œ์— depth 1์ธ str์€ '1'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฑํŠธ๋ž˜ํ‚น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ข‹์€ ์ˆ˜์—ด์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜ ์ด๋ฏ€๋กœ

๋งจ ์•ž์ž๋ฆฌ๋Š” 1์ผ ๊ฒƒ์ด๋‹ค. ๋งค ํ•จ์ˆ˜๋งˆ๋‹ค ์ง€๊ธˆ๊นŒ์ง€ ๋งŒ๋“ค์–ด์ง„ ์ˆ˜์—ด์ด ์ข‹์€ ์ˆ˜์—ด์ธ์ง€ ํŒ๋ณ„ํ•˜๊ณ  ์•„๋‹ˆ๋ผ๋ฉด retrun ํ•˜๋Š”

๋ฐฉ์‹์œผ๋กœ ์‹œ๊ฐ„์„ ์กฐ๊ธˆ์ด๋ผ๊ณ  ์ค„์ด๋ คํ–ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int N;
vector<char> alpha;
 
void backtracking(int depth, string str) {
    for (int i = 1; i <= str.length() / 2; i++) {
        for (int j = 0; j < str.length() / i - 1; j++) {
            if (str.substr(j, i) == str.substr(j + i, i))
                return;
        }
    }
    if (depth == N) {
        cout << str << "\n";
        exit(0);
    }
    if (str[str.length() - 1== '1') {
        backtracking(depth + 1, str + '2');
        backtracking(depth + 1, str + '3');
    }
    else if (str[str.length() - 1== '2') {
        backtracking(depth + 1, str + '1');
        backtracking(depth + 1, str + '3');
    }
    else if (str[str.length() - 1== '3') {
        backtracking(depth + 1, str + '1');
        backtracking(depth + 1, str + '2');
    }
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N;
 
    string temp = "";
    backtracking(1, temp + '1');
    return 0;
}
 
 
cs

ใ…‡์•„๋ฌดํŠผ ์ด๋ ‡๊ฒŒ ํ–ˆ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ๋‚˜๋Š” ํŒ๋ณ„ํ•˜๋Š” ์‹œ์ ์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋‚˜ ์ƒ๊ฐํ•ด์„œ string์˜ ๊ธธ์ด๊ฐ€ N์ด ๋์„ ๋•Œ ์ข‹์€ ์ˆ˜์—ด์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ํ•ด๋ดค๋Š”๋ฐ ์ด๊ฑด ์ค‘๊ฐ„์— ์ด๋ฏธ ์ข‹์€์ˆ˜์—ด์ด ์•„๋‹ˆ์–ด๋„ string์˜ ๊ธธ์ด๊ฐ€ N์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฌด์กฐ๊ฑด ๊ฐ€์•ผํ•˜๋‹ˆ๊นŒ ๋”๋”์šฑ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ์œ ๋ฐœํ–ˆ๋‹ค. ๋ฌธ์ œ๋Š” ํŒ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์žˆ์—ˆ๋‹ค.

๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ํ˜„์žฌ ์ˆ˜์—ด์ด 123123์ด๋ฉด 1๊ณผ2, 2์™€3, 3๊ณผ1, ... , 2์™€3๊นŒ์ง€ ํ•œ ์นธ์”ฉ ๋น„๊ตํ•˜๊ณ  12์™€31, 23๊ณผ 12, ... , 31๊ณผ 23๊นŒ์ง€ ์ด๋ ‡๊ฒŒ ๋ชจ๋‘ ๋น„๊ตํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ์ˆ˜ ๋ฐ–์— ์—†์—ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int N;
vector<char> alpha;
 
void backtracking(int depth, string str) {
    for (int i = 1; i <= str.length() / 2; i++) {
        if (str.substr(str.length() - i, i) == str.substr(str.length() - i * 2, i))
            return;
    }
    if (depth == N) {
        cout << str << "\n";
        exit(0);
    }
    if (str[str.length() - 1== '1') {
        backtracking(depth + 1, str + '2');
        backtracking(depth + 1, str + '3');
    }
    else if (str[str.length() - 1== '2') {
        backtracking(depth + 1, str + '1');
        backtracking(depth + 1, str + '3');
    }
    else if (str[str.length() - 1== '3') {
        backtracking(depth + 1, str + '1');
        backtracking(depth + 1, str + '2');
    }
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N;
 
    string temp = "";
    backtracking(1, temp + '1');
    return 0;
}
 
 
cs

๋งจ ๋’ค์— ์žˆ๋Š” ์ˆซ์ž๊ฐ€ ํฌํ•จ๋˜๊ฒŒ ๊ธธ์ด 1๋งŒํผ 2๋งŒํผ ... string์˜ ๊ธธ์ด / 2๋งŒํผ ๋งŒ ๋น„๊ตํ•˜๊ณ  ์ข‹์€ ์ˆ˜์—ด์ด ์•„๋‹ˆ๋ฉด return ํ•˜๋ฉด ๋œ๋‹ค. ๋งŒ์•ฝ 123123์ด๋ผ๋ฉด ๋งจ ๋’ค์—์žˆ๋Š” 2์™€3, ๊ทธ๋ฆฌ๊ณ  31๊ณผ 23 ๊ทธ๋ฆฌ๊ณ  123๊ณผ 123๋งŒ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค. ์ด๋ฏธ ๊ทธ ์•ž์—๋Š” ๊ทธ ์ „์— ๋‹ค ํŒ๋ณ„์„ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋˜ ํ•˜๋ฉด ์ค‘๋ณต์ธ ๊ฒƒ์ด๋‹ค. 121231์ธ str์€ ๋งŒ๋“ค์–ด ์งˆ ์ˆ˜ ์—†๋‹ค. ์™œ๋ƒ ๊ทธ ์ „์— ์ด๋ฏธ 1212๊ฐ€ ๋์„ ๋•Œ return ๋์„ ๊ฒƒ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.  

728x90

'๋ฐฑ์ค€ > Gold' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ] 1707 : ์ด๋ถ„ ๊ทธ๋ž˜ํ”„  (0) 2022.01.21
[BOJ] 1579 : ์•ฑ  (0) 2022.01.20
[BOJ] 10942 : ํŒฐ๋ฆฐ๋“œ๋กฌ?  (0) 2022.01.20
[BOJ] 2629 : ์–‘ํŒ”์ €์šธ  (0) 2022.01.19
[BOJ] 1799 : ๋น„์ˆ  (0) 2022.01.19