Skip to content

Conversation

@dohyeondol1
Copy link
Contributor

@dohyeondol1 dohyeondol1 commented Jul 10, 2025

πŸ”— 문제 링크

접두사

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

20m

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

μš”μ¦˜ λ‚œμ΄λ„κ°€ μžˆλŠ” PR이 κ½€ μ˜¬λΌμ™€μ„œ λ‚œμ΄λ„ μ™„ν™”...μ°¨μ›μ—μ„œ 쑰금 μ‰¬μš΄ 문제λ₯Ό κ°€μ Έμ™”μŠ΅λ‹ˆλ‹€. λ„ˆλ¬΄ μ‰¬μš΄κ°€..?
제 풀이 말고도 λ‹€λ₯Έ 풀이가 λ§Žμ•„λ³΄μ—¬μ„œ μ—¬λŸ¬λΆ„μ€ μ–΄λ–»κ²Œ μ ‘κ·Όν•˜μ‹€μ§€ κΆκΈˆν•œ λ§ˆμŒμ— 가져와 λ΄€μŠ΅λ‹ˆλ‹€.

μˆ˜λ„ μ½”λ“œ
main
    μ •μˆ˜ N μ„ μ–Έ 및 μž…λ ₯ λ°›κΈ°
    반볡 i = 0λΆ€ν„° N-1κΉŒμ§€
        λ¬Έμžμ—΄ arr[i] μž…λ ₯λ°›κΈ°

    arr 배열을 μ •λ ¬ν•˜κΈ° (μ‚¬μ „μˆœ)

    count = N으둜 μ„€μ •

    반볡 i = 0λΆ€ν„° N-2κΉŒμ§€
        prefix = arr[i+1]의 μ•žμ—μ„œ arr[i]의 길이만큼 자λ₯Έ λΆ€λΆ„ λ¬Έμžμ—΄
        λ§Œμ•½ prefix와 arr[i]κ°€ κ°™λ‹€λ©΄
            count = count - 1

    count 좜λ ₯

μ—¬λŸ¬ 단어가 μ£Όμ–΄μ‘Œμ„ λ•Œ, λ‹€λ₯Έ λ‹¨μ–΄μ˜ 접두어가 λ˜μ§€ μ•ŠλŠ” μ§‘ν•©μ˜ μ΅œλŒ€ 크기λ₯Ό κ΅¬ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.
μ΄λŠ” 정렬을 μ‚¬μš©ν•˜λ©΄ κ°„λ‹¨ν•˜κ²Œ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ§Œμ•½ ν•œ λ‹¨μ–΄μ˜ 접두어가 λ˜λŠ” 단어가 μžˆλ‹€λ©΄,
ν•΄λ‹Ή λ‹¨μ–΄λŠ” μ‚¬μ „μˆœ μ •λ ¬ μ‹œ 항상 접두어λ₯Ό ν¬ν•¨ν•˜λŠ” 단어 μ•žμ— μœ„μΉ˜ν•˜κ²Œ λ©λ‹ˆλ‹€.

예제 μž…λ ₯ 1을 μ˜ˆμ‹œλ‘œ λ“€μ–΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

(μž…λ ₯순)
hello
hi
h
run
rerun
running
(μ‚¬μ „μˆœ)
h
hello
hi
rerun
run
running

μ‚¬μ „μˆœμœΌλ‘œ μ •λ ¬ μ‹œ, hello의 접두어가 λ˜λŠ” hλŠ” hello의 λ°”λ‘œ μ•žμ— μœ„μΉ˜ν•˜λŠ” 것을 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.
runκ³Ό running 도 λ§ˆμ°¬κ°€μ§€μ£ .

이 경우, λ°˜λ³΅λ¬Έμ„ μ΄μš©ν•œ 브루트포슀 탐색을 톡해 ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ‹€μŒ 인덱슀의 λ‹¨μ–΄μ—μ„œ ν˜„μž¬ 인덱슀의 단어 길이만큼 μž˜λΌλ‚Έ λ¬Έμžμ—΄μ΄ ν˜„μž¬ 단어와 κ°™λ‹€λ©΄,
전체 단어 κ°œμˆ˜μ—μ„œ 1을 λΉΌλŠ” λ°©μ‹μœΌλ‘œ 풀이할 수 μžˆμŠ΅λ‹ˆλ‹€.

μ΄λ•Œ μ œμ™Έν•˜λŠ” λ‹¨μ–΄λŠ” 접두어가 될 수 μžˆλŠ” 단어가 λ©λ‹ˆλ‹€.
μ•žμ„œ 예제 μž…λ ₯ 1μ—μ„œ ν™•μΈν•œ hλŠ”, hello의 접두어가 될 μˆ˜λ„ μžˆμ§€λ§Œ hi의 접두어가 될 μˆ˜λ„ 있기 λ•Œλ¬Έμ΄μ£΅.

결과적으둜 브루트포슀λ₯Ό μ΄μš©ν•΄ 전체 단어 κ°œμˆ˜μ—μ„œ 접두어가 될 수 μžˆλŠ” λ‹¨μ–΄λ“€λ§Œ μ œμ™Έν•˜λ©΄ ν•΄κ²°ν•  수 μžˆλŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

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

@Fnhid
Copy link
Collaborator

Fnhid commented Jul 11, 2025

μ €λŠ” 트라이 자료ꡬ쑰λ₯Ό μ΄μš©ν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
image
트리의 λ³€ν˜• 버전인데 이런 μ‹μœΌλ‘œ μƒκ²¨μ„œ 접두사λ₯Ό μ‰½κ²Œ 뢄석할 수 μžˆμŠ΅λ‹ˆλ‹€.

#include <iostream>
#include <string>
#include <memory>
#include <vector>

using namespace std;

struct Trie
{
    Trie* next[26];
    bool isEnd;
    Trie(){
        isEnd=false;
        fill(next, next+26, nullptr);
    }
    ~Trie()
    {
        for (int i=0;i<26;i++)
        {
            if (next[i]) delete next[i];
        }
    }
    void insert(const string& s)
    {
        Trie* node = this;
        for (char c : s)
        {
            int idx = c - 'a';
            if (!node->next[idx]) node->next[idx] = new Trie();
            node = node->next[idx];
        }
        node->isEnd = true;

    }
    int countLeaf()
    {
        int cnt = 0;
        bool hasChild = false;
        for (int i=0;i<26;i++)
        {
            if (next[i])
            {
                hasChild = true;
                cnt += next[i]->countLeaf();
            }
        }
        if (!hasChild && isEnd) return 1;
        return cnt;
    }
};


int main()
{
    int n;

    cin >> n;
    vector<string> words(n);
    for (int i = 0; i < n; i++) cin >> words[i];
    Trie* root = new Trie();
    for (int i=0;i<n;i++) root->insert(words[i]);

    cout << root->countLeaf() << "\n";
    delete root;
}

@dohyeondol1
Copy link
Contributor Author

@Fnhid
νŠΈλΌμ΄κΉŒμ§€ μ‚¬μš©ν•œ ν’€μ΄λŠ” ν™•μ‹€νžˆ μ˜ˆμƒ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€ γ…‹γ…‹..
트라이λ₯Ό μ‚¬μš©ν•œ λ¬Έμ œλŠ” ν’€μ–΄λ³Έ 적 μ—†λŠ”λ° ν•œλ²ˆ λ°°μ›Œλ΄μ•Όκ² μ–΄μš©

@froglike6
Copy link
Collaborator

μ €λŠ” 파이썬의 startswithλ₯Ό μ΄μš©ν•΄ 이 문제λ₯Ό ν•΄κ²°ν•΄λ΄€μŠ΅λ‹ˆλ‹€. μ •λ ¬ ν›„ forλ₯Ό λŒλ©΄μ„œ startswith둜 κ²€μ‚¬ν•œ ν›„ ansλ₯Ό μΆ”κ°€ν•˜λŠ” λ°©λ²•μœΌλ‘œ μ§„ν–‰ν–ˆμŠ΅λ‹ˆλ‹€.

import sys

input = sys.stdin.readline
n = int(input())
words = [input().rstrip() for _ in range(n)]
words.sort()
ans = 0
for i, w in enumerate(words):
    if i == n - 1 or not words[i + 1].startswith(w):
        ans += 1
print(ans)

@dohyeondol1
Copy link
Contributor Author

dohyeondol1 commented Jul 13, 2025

μ½”λ“œ 길이 μ‹€ν™”ν‹°λΉ„..? startswithλŠ” μ²˜μŒλ“€μ–΄λ³΄λ„€μš”..!!

Copy link
Collaborator

@hadongun hadongun left a comment

Choose a reason for hiding this comment

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

파이썬 !! 에 μžˆλŠ” startswith λΌλŠ” ν•¨μˆ˜κ°€ λ‚΄μž₯λ˜μ–΄ μžˆλ”κ΅°μš”..!

if savedWord.startswith(currentWord):

savedWordκ°€ currentWord둜 μ‹œμž‘ν•˜λŠ”μ§€ κ²€μ‚¬ν•΄λΌλŠ” λœ»μž…λ‹ˆλ‹€!!

맀우 νŽΈλ¦¬ν•˜κ΅°μš”!

파이파이

word_count = int(input())
wordList = [input().strip() for _ in range(word_count)]

wordList.sort(key=lambda word: -len(word))

nonPrefixWords = []

for current_Word in wordList:
    is_prefix = False
    for savedWord in nonPrefixWords:
        if savedWord.startswith(current_Word):
            is_prefix = True
            break
    if not is_prefix:
        nonPrefixWords.append(current_Word)

print(len(nonPrefixWords))

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.

5 participants