본문 바로가기

Algorithm/BAEKJOON

[BOJ] 19583번 싸이버개강총회 (C++)

728x90
반응형

https://www.acmicpc.net/problem/19583

 

19583번: 싸이버개강총회

첫번째 줄에는 개강총회를 시작한 시간 S, 개강총회를 끝낸 시간 E, 개강총회 스트리밍을 끝낸 시간 Q가 주어진다. (00:00 ≤ S < E < Q ≤ 23:59) 각 시간은 HH:MM의 형식으로 주어진다. 두번째 줄부터는

www.acmicpc.net

 

#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	string t1, t2, t3, time, name;
	int t, cnt = 0;
	
	cin >> t1 >> t2 >> t3;
	set<string> first;
	int s = 60 * stoi(t1.substr(0, 2)) + stoi(t1.substr(3, 2));
	int e = 60 * stoi(t2.substr(0, 2)) + stoi(t2.substr(3, 2));
	int q = 60 * stoi(t3.substr(0, 2)) + stoi(t3.substr(3, 2));

	while(cin >> time >> name){
		t = 60 * stoi(time.substr(0, 2)) + stoi(time.substr(3, 2));
		
		if(t <= s){
			first.insert(name);
		}
		
		if(t >= e && t <= q){
			if(first.find(name) != first.end()){
				cnt++;
				first.erase(name);
			}
		}
	}
	
	cout << cnt << endl;
	return 0;
}

 

시간이 HH:MM 형식으로 주어지므로 시간은 substr(0, 2), 분은 substr(3, 2)이고 문자열이므로 stoi를 통해 문자로 변환해주어야 한다. 1시간 = 60분 이므로 60 * 시간 + 분으로 바꾸어 비교한다. 

 

채팅 기록(시간, 이름)을 한 줄씩 입력받아

시간이 개강총회를 시작하기 전이면 first에 이름을 저장한다. 이 때 set stl은 중복을 허용하지 않기 때문에 이름이 중복되어 저장되지 않는다.

시간이 개강총회를 끝내고 나서, 스트리밍을 끝낼 때까지이면 first에 이름이 있는지 확인해서 있으면 cnt를 증가시키고 중복해서 세면 안되기 때문에 first에 있는 해당 이름을 삭제한다.

728x90
반응형