※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 3277번 문제인 DOMAINS이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/3277
3277번: DOMAINS
The first line of input file contains a natural number N, 1 ≤ N ≤ 100, the number of addresses. Each of the following N lines contains one simplified address. A simplified address may begin with a prefix 'http://', after which comes one or more words s
www.acmicpc.net
주어지는 주소에서 문제에서 요구하는 문자열을 파싱해 가장 많이 등장한 문자열을 찾는 문제이다.
글쓴이는 다음과 같은 순서로 주소에서 필요한 부분을 파싱했다.
(1) 주어진 주소의 앞에 "https://"가 앞에 존재한다면 제거, 주소의 맨 뒤에 '/' 추가
(2) 앞에서부터 읽어나가며 '/'을 발견하기 전 '.'을 발견한다면 문자열의 맨 앞부터 해당 '.'까지를 제거
(3) '/'을 발견하면 문자열의 맨 앞부터 '/' 전까지가 파싱하려는 문자열이 된다.
주어지는 문자열을 multiset 자료구조로 관리하면 각 문자열이 몇번씩 등장했는지를 쉽게 알아낼 수 있다. 또한 중복없는 출력을 위해 set 자료구조를 이용해 편하게 구현할 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <vector>
#include <string>
#include <set>
using namespace std;
int N;
multiset<string> st;
int cnt;
set<string> ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
while (N--) {
string s; cin >> s; s += '/';
if (s.length() > 6 && s.substr(0, 7) == "http://") s.erase(s.begin(), s.begin() + 7);
while (1) {
int idx = 0;
while (s[idx] != '.' && s[idx] != '/') idx++;
if (s[idx] == '.') s.erase(s.begin(), s.begin() + idx + 1);
else {
st.insert(s.substr(0, idx));
break;
}
}
}
for (auto& x : st) {
if (st.count(x) > cnt) {
cnt = st.count(x);
ans.clear();
ans.insert(x);
}
else if (st.count(x) == cnt) ans.insert(x);
}
cout << cnt << '\n';
for (auto& x : ans) cout << x << ' ';
}
'BOJ' 카테고리의 다른 글
[BOJ 3285 // C++] DECODE (0) | 2023.08.31 |
---|---|
[BOJ 3278 // C++] EXCHANGE (0) | 2023.08.30 |
[BOJ 2082 // C++] 시계 (0) | 2023.08.28 |
[BOJ 2079 // C++] 팰린드롬 (1) | 2023.08.27 |
[BOJ 3279 // C++] DOLLARS (0) | 2023.08.26 |