8000 Merge pull request #462 from neppiness/16113 · kwon5346/basic-algo-lecture@cf268a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf268a6

Browse files
Merge pull request encrypted-def#462 from neppiness/16113
update: Appendix A. 16113.cpp
2 parents 685fc07 + 259daa2 commit cf268a6

File tree

1 file changed

+86
-4
lines changed

Appendix A/solutions/16113.cpp

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,93 @@
1-
// Authored by : BaaaaaaaaaaarkingDog
1+
// Authored by : scsc3204
22
// Co-authored by : -
3-
// http://boj.kr/****************
3+
// http://boj.kr/5c4deba6615742479d05f71069754f6a
44
#include <bits/stdc++.h>
55
using namespace std;
66

7-
int main(void){
7+
const string NUMBERS[10][5] = {
8+
{"###", "#.#", "#.#", "#.#", "###"}, // 0
9+
{"#", "#", "#", "#", "#"}, // 1
10+
{"###", "..#", "###", "#..", "###"}, // 2
11+
{"###", "..#", "###", "..#", "###"}, // 3
12+
{"#.#", "#.#", "###", "..#", "..#"}, // 4
13+
{"###", "#..", "###", "..#", "###"}, // 5
14+
{"###", "#..", "###", "#.#", "###"}, // 6
15+
{"###", "..#", "..#", "..#", "..#"}, // 7
16+
{"###", "#.#", "###", "#.#", "###"}, // 8
17+
{"###", "#.#", "###", "..#", "###"}, // 9
18+
};
19+
20+
int n;
21+
string s, b[5];
22+
vector<string> splitted_str[5];
23+
24+
vector<string> split(string& s, vector<int>& bl_idx) {
25+
vector<string> ret;
26+
int pos = 0;
27+
for (int nxt_pos : bl_idx) {
28+
if (nxt_pos - pos > 0)
29+
ret.push_back(s.substr(pos, nxt_pos - pos));
30+
pos = nxt_pos + 1; // 1: 구분자 최소 크기
31+
}
32+
ret.push_back(s.substr(pos, s.size() - pos));
33+
return ret;
34+
}
35+
36+
void analyze_array(int idx) {
37+
for (int no = 0; no < 10; no++) {
38+
bool is_found = 1;
39+
for (int i = 0; i < 5; i++) {
40+
if (splitted_str[i][idx] == NUMBERS[no][i]) continue;
41+
is_found = 0; break;
42+
}
43+
if (!is_found) continue;
44+
cout << no; return;
45+
}
46+
}
47+
48+
int main() {
849
ios::sync_with_stdio(0);
950
cin.tie(0);
51+
52+
cin >> n >> s;
53+
n /= 5;
54+
55+
for (int i = 0; i < 5; i++)
56+
b[i].resize(n);
57+
58+
int idx = 0;
59+
for (int i = 0; i < 5; i++)
60+
for (int j = 0; j < n; j++)
61+
b[i][j] = s[idx++];
1062

11-
}
63+
vector<int> bl_idx;
64+
for (int j = 0; j < n; j++) {
65+
bool is_all_blank = 1;
66+
for (int i = 0; i < 5; i++)
67+
if (b[i][j] != '.') is_all_blank = 0;
68+
if (is_all_blank) bl_idx.push_back(j);
69+
}
70+
71+
for (int i = 0; i < 5; i++)
72+
splitted_str[i] = split(b[i], bl_idx);
73+
74+
for (int idx = 0; idx < splitted_str[0].size(); idx++)
75+
analyze_array(idx);
76+
}
77+
/*
78+
문자열 s에는 입력으로 주어지는 시그널을 받는다.
79+
이를 5줄로 쪼개서 문자열 배열 b에 넣는다(58-61번째 줄).
80+
81+
bl_idx는 공백이 한 줄로 나오는 인덱스를 저장한다(63-69번째 줄).
82+
bl_idx를 활용하여 문자열 벡터 splitted_str로 각 행을 구분한다(71-72번쨰 줄).
83+
84+
split 함수는 강의의 구현을 응용한다.
85+
nxt_pos를 find로 찾는 게 아니라
86+
이전에 기록한 bl_idx에서 받아서 구분한다.
87+
이때, 마지막으로 확인된 bl_idx에는 size에 해당하는 인덱스가 없으므로
88+
32번째 줄과 같이 substr을 하나 추가한다.
89+
90+
7-18번째 줄에 NUMBERS를 const string으로 선언하고,
91+
이후 반환받은 splitted_str에 대해 analyze_array 함수로
92+
splitted_str과 비교하여 모든 행이 매칭되는 숫자를 출력한다(36-46번째 줄).
93+
*/

0 commit comments

Comments
 (0)
0