티스토리 뷰

 

 

뭔 생각이 부족해서 못 풀었나

일단 링별로 이름을 추가한다는 생각을 못함.

그걸로 사람수가 2명이면 커플을 출력하면 된다는 생각을 했어야함.

 

뭘 실수해서 틀렸나

페어에 대한 이해도 부족? 정도 실수는 없었음.

 

그래서 뭘 기억하면 되나

페어로 2개의 구조체를 묶어 출력할수 있음.

string, vector<string> 으로 구현해서 각 반지마다 이름들을 적립하는 방법으로 가야한다는 접근법 기억.

 

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

int main(){
    cin.tie(0) -> sync_with_stdio(0);
    int n;
    cin >> n;

    unordered_map<string, vector<string> > ring_map;

    for (int i = 0; i < n; i++){
        string name, ring; 
        cin >> name >> ring;

        if (ring != "-"){
            ring_map[ring].push_back(name);
        }
    }

    vector<pair<string, string> > couples;

    for (auto entry : ring_map){
        vector<string> names = entry.second;
        if (names.size() == 2){
            for (int i = 0; i < names.size(); i++){
                for (int j = i + 1; j < names.size(); j++){
                    couples.push_back(pair<string, string>(names[i], names[j]));
                }
            }
        }
    }

    cout << couples.size() << '\n';
    
    for (int i = couples.size() - 1; i >= 0; i--) {
        cout << couples[i].first << " " << couples[i].second << '\n';
    }

}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/11   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
글 보관함