알고리즘
백준 2002번 추월 C++
영춘권의달인
2021. 12. 5. 12:52
들어간 차들은 벡터를 이용해 순서대로 문자열을 저장해서 들어간 순서로 차의 번호를 접근할 수 있게 만들었고,
나온 차들은 unordered_map<string, int> 를 이용해 차의 번호로 나온순서를 접근할 수 있게 만들었다.
들어간 차들을 검사하면서, 자신보다 먼저 들어온 차들 중 하나라도 자기보다 늦게 나왔다면 추월을 한 것이다.
들어간 차들을 순서대로 검사하면서 그 차의 번호를 이용하여 나온 차들의 순서를 접근하여 비교하면 문제를 해결할 수 있다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<string> v;
unordered_map<string, int> car_out;
int n;
int cnt = 0;
string str;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> str;
v.push_back(str);
}
for (int i = 0; i < n; i++) {
cin >> str;
car_out.insert({ str,i });
}
for (int i = 0; i < n; i++) {
string mycar = v[i];
for (int j = 0; j < i; j++) {
string othercar = v[j];
if (car_out[mycar] < car_out[othercar]) {
cnt++;
break;
}
}
}
cout << cnt;
};