[프로그래머스] 달리기 경주
풀이
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<string> solution(vector<string> players, vector<string> callings) {
map<string, int> playerPosMap = map<string, int>();
vector<string> posPlayerVec = vector<string>();
for(int i = 0; i<players.size(); ++i){
playerPosMap[players[i]] = i;
posPlayerVec.push_back(players[i]);
}
for(int i = 0; i<callings.size(); ++i){
string player1 = callings[i];
int pos = playerPosMap[player1];
string player2 = posPlayerVec[pos-1];
posPlayerVec[pos-1] = player1;
posPlayerVec[pos] = player2;
playerPosMap[player1] = pos-1;
playerPosMap[player2] = pos;
}
return posPlayerVec;
}