백준

[백준] 13414번 수강신청 (C++, 해시)

해랑쓰 2021. 10. 6. 11:56
728x90
반응형

큐에다가 순서대로 집어넣는데 만약 같은 사람이 한번 더 신청하면 인덱스번호를 변경해서 큐에다 한번 더 넣는다.

 

나중에 큐에서 뺄때 해시에 저장된 인덱스번호와 다르면 그냥 넘어가고 같으면 해당 순서가 맞으므로 

출력한다.

 

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;

int main(void) {

	ios::sync_with_stdio(false);
	cin.tie(NULL);


	int k, l;
	unordered_map<string, int> hash;
	cin >> k >> l;
	string num;
	queue<pair<string,int>> q;
	for (int i = 0; i < l; i++) {
		cin >> num;
		q.push({ num,i });
		if (hash.find(num) != hash.end()) {
			hash[num] = i;
		}
		else {
			hash.insert({ num,i });
		}
	}
	int cnt = 0;
	while (!q.empty()) {
		string x = q.front().first;
		int idx = q.front().second;
		q.pop();
		if (hash[x] == idx) {
			cout << x << "\n";
			cnt++;
		}
		
		if (cnt == k)break;
	}


	return 0;
}
728x90
반응형