์๊ฐ ์ ํ | ๋ฉ๋ชจ๋ฆฌ ์ ํ | ์ ๋ต ๋น์จ |
2 ์ด | 128 MB | 46.610% |
๋ฌธ์
ํด๊ฒฐ
key point, 'ํฐ๋ฆฐ๋๋กฌ?' ๋ฌธ์ ๋ฅผ ํ์ฉ, d[i]๋ i๋ฒ์งธ๊น์ง ๋ฌธ์์ด์ ํฐ๋ฆฐ๋๋กฌ ๋ถํ ์ต์๊ฐ. DP
- j๋ฒ์งธ ๋ถํฐ i๋ฒ์งธ๊น์ง ํฐ๋ฆฐ๋๋กฌ์ธ์ง ์๋์ง๋ฅผ ์ ์ฅํ ๋ฐฐ์ด c[j][i]๋ฅผ ํ์ฉํด์ → ํฐ๋ฆฐ๋๋กฌ? ๋ฌธ์ ํ์ฉ
- j~i๊น์ง์ ๋ฌธ์์ด์ด ํฐ๋ฆฐ๋๋กฌ์ด๋ผ๋ฉด → c[j][i] == true
- d[i]๋ d[j - 1] + 1์ด๋ค.
- d[j - 1]์ ์ต์๊ฐ์ ๊ตฌํ๊ธฐ ์ํด์ ์กฐ๊ฑด๋ฌธ์ด ์ถ๊ฐ๋จ
์ฝ๋
๋ฉ๋ชจ๋ฆฌ | ์๊ฐ |
8140 KB | 36 ms |
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
|
#include <iostream>
#include <string>
using namespace std;
bool c[2501][2501];
int d[2501];
int main() {
string s;
cin >> s;
int n = s.size();
s = " " + s;
for (int i = 1; i <= n; i++)
c[i][i] = true;
for (int i = 1; i <= n - 1; i++) {
if (s[i] == s[i + 1]) {
c[i][i + 1] = true;
}
}
for (int k = 2; k < n; k++) {
for (int i = 1; i <= n - k; i++) {
int j = i + k;
c[i][j] = (s[i] == s[j] && c[i + 1][j - 1]);
}
}
d[0] = 0;
for (int i = 1; i <= n; i++) {
d[i] = -1;
for (int j = 1; j <= i; j++) {
if (c[j][i]) {
if (d[i] == -1 || d[i] > d[j - 1] + 1) {
d[i] = d[j - 1] + 1;
}
}
}
}
printf("%d", d[n]);
return 0;
}
|
cs |
'๐ฅ PS(Problem Solving) ๐ฅ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] #11505 ๊ตฌ๊ฐ ๊ณฑ ๊ตฌํ๊ธฐ (0) | 2021.02.15 |
---|---|
[BOJ] #10868 ์ต์๊ฐ (0) | 2021.02.15 |
[BOJ] #10942 ํฐ๋ฆฐ๋๋กฌ? (0) | 2021.02.09 |
[BOJ] #2632 ํผ์ํ๋งค (0) | 2021.02.05 |
[BOJ] #1208 ๋ถ๋ถ์์ด์ ํฉ 2 (0) | 2021.02.04 |
๋๊ธ