2002 - 추월

2025. 2. 28. 20:26PS/백준

문제

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

느낀점

구현 기본 문제, 처음에는 갯수로 접근했는데, 직관적으로 set으로 풀면 되는 문제였다. 로직 자체를 생각하는 것은 어렵지 않았다.

풀이

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
#define psi pair<string, int>
int main()
{
    int N;
    cin >> N;
    vector<psi> entrance(N);
    vector<psi> exit(N);

    for (int i = 0; i < N; i++)
    {
        string temp;
        cin >> temp;
        entrance[i] = make_pair(temp, i);
    }

    for (int i = 0; i < N; i++)
    {
        string temp;
        cin >> temp;
        exit[i] = make_pair(temp, i);
    }

    int cnt = 0;

    set<string> exceed;
    set<string> forward;
    for (int i = 0; i < N; i++)
    {
        psi en = entrance[i];
        forward.insert(en.first);
        for (int j = 0; j < N; j++)
        {
            psi ex = exit[j];
            if (en.first == ex.first)
            {
                break;
            }
            else
            {
                if (find(forward.begin(), forward.end(), ex.first) == forward.end() &&
                    find(exceed.begin(), exceed.end(), ex.first) == exceed.end())
                {

                    exceed.insert(ex.first);
                }
            }
        }
    }

    cout << exceed.size();

    return 0;
}

'PS > 백준' 카테고리의 다른 글

1986 - 체스  (0) 2025.02.26
1713 - 후보 추천하기  (0) 2025.02.26
1421 - 나무꾼 이다솜  (0) 2025.02.26
1283 - 단축키 지정  (0) 2025.02.26
28066 - 타노스는 요세푸스가 밉다  (0) 2025.02.25