Skip to content

Conversation

@hadongun
Copy link
Collaborator

πŸ”— 문제 링크

λΉ„λ°€λ²ˆν˜Έ μ°ΎκΈ°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

37min

✨ μˆ˜λ„ μ½”λ“œ

자료ꡬ쑰 λ§ˆμ§€λ§‰ μ‹œκ°„μ— ν•΄μ‹œ? 에 λŒ€ν•΄ λ°°μ› λ˜ 것 κ°™μ•„μ„œ 볡슡 κ²Έ κ΄€λ ¨ 문제λ₯Ό ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.

image

μ²˜μŒμ— ν’€ λ•Œ κ·Έλƒ₯ μ‚¬μ΄νŠΈ, λΉ„λ°€λ²ˆν˜Έλ₯Ό νŠœν”Œλ‘œ μ €μž₯ν•΄μ„œ 맀번 μˆœνšŒν•˜μ—¬ 찾으렀 ν–ˆμœΌλ‚˜, μ‹œκ°„μ΄ 맀우 였래 걸릴 것 κ°™μ•„μ„œ
쑰금 μ°Ύμ•„λ³΄μ•˜λŠ”λ° 이전에도 μ‚¬μš©ν–ˆμ—ˆλ˜ λ”•μ…”λ„ˆλ¦¬λ₯Ό 톡해 κ΅¬ν˜„ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

정말 κ΅¬ν˜„μ΄ μ•ˆ λ˜μ–΄ μžˆλŠ”κ²Œ μ—†λŠ” νŒŒμ΄μ¬μ΄λ„€μš”

site와 passwordλ₯Ό λ”°λ‘œ μž…λ ₯ 받은 ν›„
passwords [site] = password 와 같이 λ”•μ…”λ„ˆλ¦¬μΈ passwords에 μ €μž₯ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

이후 λΉ„λ°€λ²ˆν˜Έλ₯Ό 찾고자 ν•˜λŠ” μ‚¬μ΄νŠΈλ₯Ό μž…λ ₯λ°›μ•„
'password[find]` λ₯Ό 좜λ ₯ν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν•˜μ˜€μŠ΅λ‹ˆλ‹€.!

λ‹€μŒμ—” μ’€ 더 μ‘μš© 문제λ₯Ό ν’€μ–΄λ³΄κ² μŠ΄λ‹Ή

μˆ˜λ„μ½”λ“œ

1. μ •μˆ˜ N, M μž…λ ₯λ°›κΈ°  ← N: μ €μž₯ν•  정보 개수, M: 찾을 μ‚¬μ΄νŠΈ 수

2. 빈 λ”•μ…”λ„ˆλ¦¬ passwords 생성

3. N번 반볡:
     - μ‚¬μ΄νŠΈ μ£Όμ†Œμ™€ λΉ„λ°€λ²ˆν˜Έ μž…λ ₯λ°›κΈ°
     - λ”•μ…”λ„ˆλ¦¬μ— μ €μž₯: passwords[μ‚¬μ΄νŠΈ] = λΉ„λ°€λ²ˆν˜Έ

4. M번 반볡:
     - μ°Ύκ³  싢은 μ‚¬μ΄νŠΈ 이름 μž…λ ₯λ°›κΈ°
     - ν•΄λ‹Ή μ‚¬μ΄νŠΈμ˜ λΉ„λ°€λ²ˆν˜Έλ₯Ό λ”•μ…”λ„ˆλ¦¬μ—μ„œ μ°Ύμ•„μ„œ 좜λ ₯

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@Fnhid
Copy link
Collaborator

Fnhid commented Jul 16, 2025

#include <iostream>
#include <map>
#include <algorithm>

using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);


    int n, m;
    string si, pw;
    cin>>n>>m;
    map<string, string> memo;

    for (int i=0;i<n;i++)
    {

        cin >> si >> pw;
        memo.insert(pair<string, string>(si, pw));

    }
    for (int i=0;i<m;i++)
    {
        cin >> si;
        cout << memo.find(si)->second << "\n";
    }

    return 0;

}

c++에 dictionaryλŠ” μ—†μ§€λ§Œ, λΉ„μŠ·ν•œ stl인 map을 μ‚¬μš©ν•΄μ„œ ν’€μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

map은 κ· ν˜• 이진 탐색 트리둜 데이터λ₯Ό κ΄€λ¦¬ν•΄μ„œ 탐색 μ‹œκ°„μ΄ O(log n)이긴 ν•˜μ§€λ§Œ,
이 문제λ₯Ό ν’€κΈ°μ—λŠ” μΆ©λΆ„ν•œ μ‹œκ°„μΈ 것 κ°™μŠ΅λ‹ˆλ‹€.

@froglike6
Copy link
Collaborator

μ—­μ‹œ νŒŒμ΄μ¬μž…λ‹ˆλ‹€. 이미 dict둜 ν•΄μ‹œκ°€ κ΅¬ν˜„λ˜μ–΄μžˆμ–΄ μ½”λ“œ μ§œκΈ°μ—λŠ” 맀우 νŽΈν•œ 것 κ°™μŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ μ–΄λ–€ μ‹μœΌλ‘œ κ΅¬ν˜„λ˜μ–΄ μžˆλŠ”μ§€ μ•Œμ•„λ΄€λŠ”λ°, 이와 같이 κ΅¬ν˜„λ˜μ–΄ μžˆλ‹€κ³  ν•©λ‹ˆλ‹€. μΆ©λŒμ„ ν•΄κ²°ν•˜λŠ” 방식이 λ…νŠΉν•œ 것 κ°™μŠ΅λ‹ˆλ‹€.

import sys
input = sys.stdin.readline

password = dict()
n, m = map(int,input().split())
for _ in range(n):
    site, pwd = input().split()
    password[site] = pwd
for _ in range(m):
    print(password[input().rstrip()])

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ§‘ν•©κ³Ό λ§΅ 문제λ₯Ό 잘 μ•ˆν’€μ–΄μ„œ ν•˜λ§ˆν„°λ©΄ κΉŒλ¨Ήμ„ λ»” ν–ˆμŠ΅λ‹ˆλ‹€ γ…Ž
μ§‘ν•©κ³Ό 맡은 닀루기가 μ–΄λ ΅μ§€ μ•Šμ•„μ„œ ν™œμš© λ¬Έμ œλ„ 재밌게 ν’€ 수 μžˆμ„ 것 κ°™μ•„μš”!

#include <iostream>
#include <map>
using namespace std;

int N, M;
string site, password;
map<string, string> memozang;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    
    cin >> N >> M;
    while(N--) {     
        cin >> site >> password;
        memozang.insert({site, password});
    }
    
    while(M--) {
        cin >> site;
        password = memozang[site];
        cout << password << '\n';
    }
    
    return 0;
}

κΉŒλ¨Ήμ„ λ•Œμ―€ 볡기되고 μ’‹λ„€μš”!
κ°„λ‹¨ν•œ 문제 μΆ”μ²œ

@hadongun hadongun merged commit 3cef7f4 into AlgoLeadMe:main Jul 18, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants