문제 풀이/문제 풀이(BOJ)

[Silver V] 귀걸이 - 1303

풀뿌리 2024. 11. 16. 17:11

[문제 위치]

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

[문제 풀이]

문자열 문제이다.

주어진 자료를 순회하며 시나리오에 해당하는 사건이 발생하는 학생을 판단하여 해당하는 학생이 있을 경우 해당 학생의 이름을 양식에 맞게 출력한다.

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;

int main() {
    ios::sync_with_stdio(false); // Disable synchronization with C I/O
    cin.tie(nullptr);           // Untie cin and cout for faster operations

    int scenario = 1;

    while (true) {
        int n;
        cin >> n;
        if (n == 0) break; // Exit condition

        vector<string> names(n);
        for (int i = 0; i < n; i++) {
            cin >> ws; // Clear leading whitespace
            getline(cin, names[i]); // Read the full name
        }

        unordered_map<int, int> counts; // Track occurrences of each student number

        // Process 2n - 1 lines of input for student numbers and actions
        for (int i = 0; i < 2 * n - 1; i++) {
            int student_number;
            char action;
            cin >> student_number >> action; // Read student number and action (A/B)
            counts[student_number]++; // Increment count for this student
        }

        // Find the student with an odd count
        for (int i = 1; i <= n; i++) {
            if (counts[i] % 2 != 0) {
                cout << scenario << " " << names[i - 1] << "\n"; // Output scenario and name
                break;
            }
        }

        scenario++; // Move to the next scenario
    }

    return 0;
}