홍시홍의 프로그래밍

[프로그래머스] 완주하지 못한자 본문

알고리즘 문제풀이/프로그래머스

[프로그래머스] 완주하지 못한자

홍시홍 2020. 3. 17. 22:32

요구사항

완주하지 못한자 탐색하기

 

풀이

문자열 탐색이다

기본적인 해쉬문제이다

참가자에서 m[string]++;

완주자에서 m[string]--;

해준 뒤

m[string]==1 인것을 출력한다

 

#include <string>
#include <vector>
#include <map>

using namespace std;
const int MAX = 100100;
const int HASHSIZE = 123571;
const int PN = 23;
map<string, int> m;

string solution(vector<string> participant, vector<string> completion) {
	string answer = "";
	for (int i = 0; i < participant.size(); i++) {
		string now = participant[i];
		m[now]++;
	}
	for (int i = 0; i < completion.size(); i++) {
		string now = completion[i];
		m[now]--;
	}
	for (auto iter = m.begin(); iter != m.end(); iter++) {
		if (iter->second == 1) {
			answer = iter->first;
		}
	}
	return answer;
}
Comments