8000 Merge pull request #467 from neppiness/6581 · kwon5346/basic-algo-lecture@9777e9e · GitHub
[go: up one dir, main page]

Skip to content

Commit 9777e9e

Browse files
Merge pull request encrypted-def#467 from neppiness/6581
update: Appendix A. 6581.cpp
2 parents a30df9d + ad45339 commit 9777e9e

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

Appendix A/solutions/6581.cpp

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

7-
int main(void){
7+
const int LIM = 80;
8+
9+
string s, input;
10+
vector<string> split_input;
11+
12+
vector<string> split(string& s, string sep) {
13+
vector<string> ret;
14+
int pos = 0;
15+
while (pos < s.size()) {
16+
int nxt_pos = s.find(sep, pos);
17+
if (nxt_pos == -1) nxt_pos = s.size();
18+
if (nxt_pos - pos > 0)
19+
ret.push_back(s.substr(pos, nxt_pos - pos));
20+
pos = nxt_pos + sep.size();
21+
}
22+
return ret;
23+
}
24+
25+
int main() {
826
ios::sync_with_stdio(0);
927
cin.tie(0);
10-
11-
}
28+
29+
while (getline(cin, s)) input += " " + s;
30+
for (char& c : input)
31+
if (c == '\t') c = ' ';
32+
33+
split_input = split(input, " ");
34+
35+
int cnt = 0;
36+
for (auto& str : split_input) {
37+
if (str != "<br>" && str != "<hr>") {
38+
if (cnt + str.size() + 1 > LIM) { cout << '\n'; cnt = 0; }
39+
if (cnt != 0) { cout << ' '; cnt++; }
40+
cout << str; cnt += str.size();
41+
} else {
42+
if (str == "<hr>") {
43+
if (cnt) cout << '\n';
44+
int no = LIM;
45+
while (no--) cout << '-';
46+
}
47+
cout << '\n'; cnt = 0;
48+
}
49+
}
50+
}
51+
/*
52+
getline으로 문자열을 한 줄씩 입력을 받으며
53+
공백문자로 구분해 input 문자열에 추가한다(29번째 줄).
54+
55+
여러 개의 탭 문자 또한 입력으로 들어오기 때문에,
56+
이는 공백 문자로 대체해준다(30-31번째 줄).
57+
58+
split 함수를 통해 공백 문자가 제거된
59+
문자열 벡터 split_input을 반환 받는다(33번째 줄).
60+
61+
이후 cnt 변수로 한 줄에 출력된 단어 개수를 카운트한다.
62+
split_input의 원소 str이 <br>이나 <hr>이 아니라면
63+
단어이므로, 아래와 같은 로직을 따라 출력한다.
64+
- 단어 개수 제한 80자를 넘으면 줄을 바꾸고 cnt를 초기화한다.
65+
- cnt가 초기화되지 않은 경우, 공백을 출력하고 cnt를 1 증가시킨다.
66+
- 단어를 출력하고, 출력한 단어의 길이만큼 cnt 값을 증가시킨다.
67+
68+
만약 <br>이나 <hr>인 경우 아래와 같은 로직을 따라 출력한다.
69+
- 공통: 줄을 바꾸고 cnt를 초기화한다.
70+
- <hr>: 현위치가 줄의 시작점이 아닌 경우(cnt가 0이 아닌 경우)
71+
줄을 바꾸고 80개 '-'를 출력한다. 만약, 줄의 시작점이라면 줄을
72+
바꾸지 않고 80개 '-'를 출력한다.
73+
74+
특별히, 줄바꿈하기 직전 단어 뒤로 공백 문자가 붙으면 오답처리되므로
75+
단어와 단어를 구분할 때만 공백 문자를 출력하도록 하자.
76+
오답 참고: http://boj.kr/dc2fa1e59f6348eea5ad5b81810570ef
77+
*/

0 commit comments

Comments
 (0)
0