Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

44-tgyuuAn #159

Merged
merged 3 commits into from
Mar 24, 2024
Merged

44-tgyuuAn #159

merged 3 commits into from
Mar 24, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Mar 13, 2024

πŸ”— 문제 링크

개미꡴

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

30λΆ„

κ·Έλƒ₯ solved.ac 6λž­ν¬μ—μ„œ μ•„λ¬΄κ±°λ‚˜ κ³¨λžλŠ”λ°,

λ¬ΈλΉˆλ‹˜μ΄ μ΅œκ·Όμ— ν’€μ—ˆλ˜ νŠΈλΌμ΄κ°€ λ°”λ‘œ λ– μ˜¬λžμŒ.

νŠΈλΌμ΄κ·Έλ¦Όμ€ μ•Œκ³ μžˆμ—ˆμ§€λ§Œ 막상 κ΅¬ν˜„ν•΄λ³΄λ €λ‹ˆ μƒκ°λŒ€λ‘œ λ˜μ§€ μ•Šμ•„μ„œ 트라이 자료ꡬ쑰 레퍼런슀λ₯Ό 보고 λ§Œλ“€μ—ˆμŒ.

아직 μƒκ°λŒ€λ‘œ 잘 λ˜μ§€ μ•Šμ•˜λ‹€. 더 λ§Žμ€ μ—°μŠ΅μ΄ ν•„μš”ν•  λ“―.

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

그림에 λ‚˜μ™€μžˆλŠ” κ·ΈλŒ€λ‘œ 트라이 자료ꡬ쑰λ₯Ό λ§Œλ“€λ©΄ λ˜λŠ” λ¬Έμ œμ΄λ‹€.

image

트라이 자료ꡬ쑰λ₯Ό μ•„λŠλƒ λͺ¨λ₯΄λŠλƒλ₯Ό νŒκ°€λ¦„ ν•˜λŠ” 문제인 것 κ°™λ‹€.







트라이 μžλ£Œκ΅¬μ‘°λŠ” #155 (comment) 에 올라온 λ¬ΈλΉˆλ‹˜ PRμ—μ„œ κΈ΄λž΅ν•˜κ²Œ μ†Œκ°œλ˜μ–΄μžˆλŠ”λ°,

μ‹€μ œλ‘œ κ΅¬ν˜„ν•΄λ³΄λ©΄ 또 λ‹€λ₯Έ λŠλ‚Œμ΄λ‹€.

이 λ•ŒλŠ” 레퍼런슀λ₯Ό 보면 μ’‹λ‹€.







자료ꡬ쑰λ₯Ό λ§Œλ“€κ³  데이터λ₯Ό λ‹€ μ‚½μž…ν–ˆλ”λΌλ©΄,

κ°€μž₯ 루트 λ…Έλ“œλΆ€ν„° 사전 순으둜 완탐을 진행해주면 λœλ‹€.

이 뢀뢄도 트라이 μžλ£Œκ΅¬μ‘°μ— λŒ€ν•œ κ°œλ…μ΄ 많이 λΆ€μ‘±ν•΄μ„œ κ΅¬ν˜„ν•˜λŠ” 데 μ‹œκ°„μ΄ μ’€ κ±Έλ Έλ‹€.







μƒˆλ‘œμš΄ μœ ν˜•μ„ μ ‘ν•΄λ³΄λ‹ˆ μž¬λ°ŒκΈ°λ„ ν–ˆμ§€λ§Œ 쑰금 νž˜λ“€κΈ°λ„ ν–ˆλ‹€.

체화할 λ•Œ κΉŒμ§€ 트라이 문제λ₯Ό μ’€ 풀어봐야겠닀.

μ΄λ²ˆμ—λŠ” 레퍼런슀λ₯Ό 보고 κ΅¬ν˜„μ„ ν–ˆμ§€λ§Œ,

λ‹€μŒ λ²ˆμ—λŠ” 레퍼런슀λ₯Ό 보지 μ•Šκ³  슀슀둜 κ΅¬ν˜„ν•΄λ΄μ•Όκ² λ”°.

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

  • 트라이 자료ꡬ쑰λ₯Ό μ•Œκ²Œλ˜μ—ˆλ‹€.
  • 직접 κ΅¬ν˜„ν•΄λ³΄λ‹ˆκΉŒ μ•„λŠ” κ±°λž‘ 또 λ‹€λ₯Έ λŠλ‚Œμ΄μ—ˆλ‹€.
  • κ³„μ†ν•΄μ„œ 풀어봐야지..

@tgyuuAn tgyuuAn added tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. μž‘μ„± 쀑 ⏱️ labels Mar 13, 2024
@tgyuuAn tgyuuAn self-assigned this Mar 13, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review March 13, 2024 14:47
@tgyuuAn tgyuuAn mentioned this pull request Mar 16, 2024
Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

λ¬ΈλΉˆλ‹˜ pr 이후에 λ°”λ‘œ 트라이 문제λ₯Ό ν’€μ–΄λ³΄μ…¨κ΅°μš” μ‹€ν–‰λ ₯ γ„·γ„·!!
트라이 κ³΅λΆ€ν•˜λ‹€κ°€ λ”•μ…”λ„ˆλ¦¬λΆ€ν„° λ‹€μ‹œ 보느라 리뷰가 쬐끔 λŠ¦μ–΄μ‘ŒμŠ΅λ‹ˆλ‹€ ... πŸ˜…

저도 λ‹€μŒμ£Ό~λ‹€λ‹€μŒμ£Ό DP 곡뢀가 λλ‚˜λ©΄ 트라이 문제 ν’€μ–΄λ³Όλ €κ³  λ¬Έμ œλ„ μ°Ύμ•„λ†¨μŠ΄λ‹€ γ…Žγ……γ…Ž

μ΄λ²ˆμ£Όλ„ λ¬Έμ œν‘ΈλŠλΌ κ³ μƒν•˜μ…¨μŠ΄λ‹€-! πŸ’ͺ


def input(): return sys.stdin.readline()

class Node():
Copy link
Collaborator

Choose a reason for hiding this comment

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

νŒŒμ΄μ¬μ—μ„œμ˜ class... 거의 잊고 μ§€λƒˆλ„€μš”
λ‹€μ‹œ class κ°œλ… μ°Ύμ•„λ³΄λ©΄μ„œ λ³΅μŠ΅ν•˜κ³  κ°‘λ‹ˆλ‹€ πŸ”₯πŸ”₯

Copy link
Collaborator

@pknujsp pknujsp left a comment

Choose a reason for hiding this comment

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

이전 λ¬Έμ œλŠ” λ¦¬ν”„λ…Έλ“œμΈμ§€ νŒλ‹¨ν•˜λŠ”κ²Œ ν•„μš”ν•΄μ„œ
μ’€ ν’€κΈ° κΉŒλ‹€λ‘œμ› λŠ”λ°
이 λ¬Έμ œλŠ” 큰 쑰건없이 νŽΈν•˜κ²Œ ν’€ 수 μžˆμ–΄μ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€.

저도 트라이 자료ꡬ쑰λ₯Ό λ”°λΌμ„œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

  • λ¨Όμ € λͺ¨λ“  μ›μ†Œλ“€μ„ νŠΈλ¦¬ν˜•νƒœλ‘œ λ§Œλ“€κ³ 
  • DFSλ₯Ό μ§„ν–‰ν•˜λŠ”λ°
  • 이 λ•Œ μ‚¬μ „μˆœμœΌλ‘œ λΆ€λͺ¨ λ…Έλ“œλ₯Ό νƒμƒ‰ν•˜λ©΄μ„œ
  • 좜λ ₯ν•  λ¬Έμžμ—΄μ„ κΉŠμ΄μ— λ”°λΌμ„œ μ μ ˆν•˜κ²Œ λ§Œλ“€μ–΄ λ”°λ‘œ λ¦¬μŠ€νŠΈμ— μΆ”κ°€ν•œλ’€μ—
  • μ΅œμ’…μ μœΌλ‘œ μ™„μ„±λœ 리슀트λ₯Ό 좜λ ₯

사전 순으둜 λ‚˜νƒ€λ‚΄μ•Ό 함을 λ¬Έμ œμ—μ„œ μš”κ΅¬ν•΄μ„œ
μ²˜μŒμ— λ„ˆλ¬΄ λ³΅μž‘ν•˜κ²Œ μƒκ°ν–ˆλŠ”λ° 막상 ν’€λ©΄μ„œ λ³΄λ‹ˆ 그리 κΉŒλ‹€λ‘­μ§€ μ•Šμ•˜κ³ 
생각보닀 λ‹¨μˆœν•˜κ²Œ ν’€μ–΄λ³Ό 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

N = int(input())
tree = {}

for _ in range(N):
    lst = input().split()

    if lst[1] not in tree:
        tree.setdefault(lst[1], {})
    node = tree[lst[1]]
    
    for i in range(2, len(lst)):
        if lst[i] not in node:
            node.setdefault(lst[i], {})
        node = node[lst[i]]

result = []

def dfs(node, depth):
    for char in sorted(node.keys()):
        depths = '--' * depth
        result.append(depths + char)
        dfs(node[char], depth + 1)

for start in sorted(tree.keys()):
    result.append(start)
    dfs(tree[start], 1)

for v in result:
    print(v)

@Munbin-Lee
Copy link
Member

#include <iostream>
#include <map>

using namespace std;

struct Tree {
    struct Node {
        map<string, Node *> children;

        Node *getOrCreate(string &name) {
            if (children.contains(name)) return children[name];
            return children[name] = new Node();
        }
    };

    void print(Node *cur, int depth = 0) {
        for (auto [k, v]: cur->children) {
            for (int i = 0; i < depth; i++) {
                cout << "--";
            }
            cout << k << '\n';
            print(v, depth + 1);
        }
    }

    Node *root = new Node;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    Tree *tree = new Tree();

    while (N--) {
        int K;
        cin >> K;

        Tree::Node *cur = tree->root;

        while (K--) {
            string t;
            cin >> t;
            cur = cur->getOrCreate(t);
        }
    }

    tree->print(tree->root);

    return 0;
}

트라이 PR 보고 λ°”λ‘œ ν’€μ–΄λ³΄λŠ” λŠ₯λ ₯μžγ„·γ„·γ„·γ„·

Copy link
Member

@Munbin-Lee Munbin-Lee left a comment

Choose a reason for hiding this comment

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

둜직이 저와 거의 κ°™μ•„μ„œ λ“œλ¦΄ 말씀이 μ—†λ„€μš”.

κ΅Ώμž…λ‹ˆλ‹€.

@tgyuuAn tgyuuAn merged commit 88a639e into main Mar 24, 2024
@tgyuuAn tgyuuAn deleted the 44-tgyuuAn branch March 24, 2024 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. 리뷰 μ™„λ£Œ βœ”οΈ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants