๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/2661
ํ์ด
์์ ์ ๋๊ตฐ๊ฐ์ ์ฝ๋๋ฅผ ๋ฐ๋ผ์น๋ฉด์ ๋์ถฉ ์ดํด๋ง ํ๊ณ ๋์ด๊ฐ๋ ๋ฌธ์ ๋ค. ์ด๋ฒ์ ๋ค์ ๋ฐฑํธ๋ํน์ ํ์ด๋ณด๋ฉด์
์ ๋๋ก ํ์ด๋ณด๊ธฐ๋ก ํ๋ค. ๋ฐฑํธ๋ํน์ผ๋ก 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 ๋์ ๊ฒ์ด๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์๊ฐ๋ณต์ก๋๋ฅผ ์ค์ผ ์ ์๋ค.
'๋ฐฑ์ค > 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 |