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

14-wkdghdwns199 #59

Merged
merged 4 commits into from
Mar 27, 2024
Merged

14-wkdghdwns199 #59

merged 4 commits into from
Mar 27, 2024

Conversation

wkdghdwns199
Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 commented Mar 13, 2024

πŸ”— 문제 링크

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

문제
ν™”μ€μ΄λŠ” 이번 μ˜μ–΄ μ‹œν—˜μ—μ„œ ν‹€λ¦° 문제λ₯Ό λ°”νƒ•μœΌλ‘œ μ˜μ–΄ 단어 μ•”κΈ°λ₯Ό ν•˜λ €κ³  ν•œλ‹€. κ·Έ κ³Όμ •μ—μ„œ 효율적으둜 μ˜μ–΄ 단어λ₯Ό μ™Έμš°κΈ° μœ„ν•΄ μ˜μ–΄ 단어μž₯을 λ§Œλ“€λ € ν•˜κ³  μžˆλ‹€. 화은이가 λ§Œλ“€κ³ μž ν•˜λŠ” 단어μž₯의 단어 μˆœμ„œλŠ” λ‹€μŒκ³Ό 같은 μš°μ„ μˆœμœ„λ₯Ό μ°¨λ‘€λ‘œ μ μš©ν•˜μ—¬ λ§Œλ“€μ–΄μ§„λ‹€.

  1. 자주 λ‚˜μ˜€λŠ” λ‹¨μ–΄μΌμˆ˜λ‘ μ•žμ— λ°°μΉ˜ν•œλ‹€.
  2. ν•΄λ‹Ή λ‹¨μ–΄μ˜ 길이가 길수둝 μ•žμ— λ°°μΉ˜ν•œλ‹€.
  3. μ•ŒνŒŒλ²³ 사전 순으둜 μ•žμ— μžˆλŠ” λ‹¨μ–΄μΌμˆ˜λ‘ μ•žμ— λ°°μΉ˜ν•œλ‹€.

M보닀 짧은 길이의 λ‹¨μ–΄μ˜ 경우 μ½λŠ” κ²ƒλ§ŒμœΌλ‘œλ„ μ™ΈμšΈ 수 있기 λ•Œλ¬Έμ— 길이가 $M$이상인 λ‹¨μ–΄λ“€λ§Œ μ™Έμš΄λ‹€κ³  ν•œλ‹€. 화은이가 괴둜운 μ˜λ‹¨μ–΄ μ•”κΈ°λ₯Ό 효율적으둜 ν•  수 μžˆλ„λ‘ 단어μž₯을 λ§Œλ“€μ–΄ 주자.

μž…λ ₯
첫째 μ€„μ—λŠ” μ˜μ–΄ 지문에 λ‚˜μ˜€λŠ” λ‹¨μ–΄μ˜ 개수 $N$κ³Ό μ™ΈμšΈ λ‹¨μ–΄μ˜ 길이 기쀀이 λ˜λŠ” $M$이 곡백으둜 κ΅¬λΆ„λ˜μ–΄ 주어진닀. ($1 \leq N \leq 100,000$, $1 \leq M \leq 10$)

λ‘˜μ§Έ 쀄뢀터 $N+1$번째 μ€„κΉŒμ§€ μ™ΈμšΈ 단어λ₯Ό μž…λ ₯λ°›λŠ”λ‹€. μ΄λ•Œμ˜ μž…λ ₯은 μ•ŒνŒŒλ²³ μ†Œλ¬Έμžλ‘œλ§Œ 주어지며 λ‹¨μ–΄μ˜ κΈΈμ΄λŠ” $10$을 λ„˜μ§€ μ•ŠλŠ”λ‹€.

단어μž₯에 단어가 λ°˜λ“œμ‹œ 1개 이상 μ‘΄μž¬ν•˜λŠ” μž…λ ₯만 주어진닀.

좜λ ₯
ν™”μ€μ΄μ˜ 단어μž₯에 λ“€μ–΄ μžˆλŠ” 단어λ₯Ό 단어μž₯의 μ•žμ— μœ„μΉ˜ν•œ 단어뢀터 ν•œ 쀄에 ν•œ 단어씩 μˆœμ„œλŒ€λ‘œ 좜λ ₯ν•œλ‹€.

예제 μž…λ ₯ 1
7 4
apple
ant
sand
apple
append
sand
sand

예제 좜λ ₯ 1
sand
apple
append

예제 μž…λ ₯ 2
12 5
appearance
append
attendance
swim
swift
swift
swift
mouse
wallet
mouse
ice
age

예제 좜λ ₯ 2
swift
mouse
appearance
attendance
append
wallet

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

30λΆ„ - dictionary μžλ£Œν˜•μ˜ μš°μ„ μˆœμœ„ 정렬을 확인해 λ³Ό 수 μžˆμ—ˆλ‹€..!

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

κ²°κ΅­ λ“€μ–΄μ˜¨ 단어듀을 λ‹€μŒκ³Ό 같은 쑰건듀을 톡해 μž…λ ₯받은 단어듀을 μ •λ ¬ν•˜λΌλŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€.

  1. 자주 λ‚˜μ˜€λŠ” 단어
  2. ν•΄λ‹Ή λ‹¨μ–΄μ˜ 길이가 길수둝 μ•žμ—
  3. μ•ŒνŒŒλ²³ 사전 순으둜 μ•žμ— μžˆλŠ” λ‹¨μ–΄μΌμˆ˜λ‘ μ•žμ—

listλ₯Ό λ‹€ λ”°λ‘œ λ”°λ‘œ μ •λ ¬ν•΄μ„œ ν•΄μ•Ό ν• κΉŒ λΌλŠ” 생각에 λ§‰λ§‰ν–ˆμ§€λ§Œ, dictionary 둜 자주 λ‚˜μ˜€λŠ” 단어λ₯Ό key κ°’μœΌλ‘œ μ‚Όμ•„ sorted ν•¨μˆ˜λ‘œ μš°μ„ μˆœμœ„ 정렬을 ν•  수 μžˆμ§€ μ•Šμ„κΉŒλΌλŠ” λ§‰μ—°ν•œ μƒκ°μœΌλ‘œ λ‹€μŒκ³Ό 같은 μ½”λ“œλ₯Ό μ§°μŠ΅λ‹ˆλ‹€.

# ν†΅κ³Όλœ μ½”λ“œ
import sys
input = sys.stdin.readline
N,M = map(int, input().split())
word_count = {}
for _ in range(N):
    word = input().rstrip()
    if len(word) >= M :
        if word in word_count :
            word_count[word][0] += 1
        else :
            word_count[word] = [1,len(word)]


sorted_word_list = sorted(word_count, key=lambda key : (-word_count[key][0], -word_count[key][1], key))
print()
for word in sorted_word_list :
    print(word) 

λ˜λ”λΌκ΅¬μš”! μ €λ²ˆμ— νƒœμ› λ‹˜μ΄ μ˜¬λ¦¬μ‹  PR μ—μ„œ λ§μ”€ν•˜μ‹  쑰건 μ •λ ¬ 방식을 μ‘μš©ν•΄μ„œ 닀쀑 쑰건을 κ±Έ μˆ˜λ„ μžˆλ‹€λŠ” 것을 μ•Œ 수 μžˆλŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€!

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

@SeongHoonC
Copy link
Collaborator

kotlin 으둜 ν‘ΈλŠ”λ° μ‹œκ°„μ΄ˆκ³Ό λ„ˆλ¬΄ μ–΅κΉŒν•΄μ„œ 포기..닡은 잘 λ‚˜μ˜΅λ‹ˆλ‹€.

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = System.out.bufferedWriter()
    val (N, M) = br.readLine().split(" ").map { it.toInt() }

    val dict = hashMapOf<String, Int>()
    for (i in 0 until N) {
        val word = br.readLine()!!
        if (word.length < M) {
            continue
        }
        dict[word] = (dict[word] ?: 0) + 1
    }
    dict.entries.sortedWith(compareBy({ -it.value }, { -it.key.length }, { it.key })).forEach {
        bw.write("${it.key}\n")
    }
    bw.flush()
    bw.close()
}

Comment on lines +8 to +17
if word in word_count :
word_count[word][0] += 1
else :
word_count[word] = [1,len(word)]

print()
for key in word_count :
print (key)

sorted_word_list = sorted(word_count, key=lambda key : (-word_count[key][0], -word_count[key][1], key))
Copy link
Member

Choose a reason for hiding this comment

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

단어 길이λ₯Ό λ”°λ‘œ 배열에 정리λ₯Ό ν•΄λ‘μ…¨κ΅°μš”!!

    else: # 단어가 M이상인 경우
        if word in word_lst: # 단어가 μžˆλŠ” 경우
            word_lst[word] += 1
        else: # 단어가 μ—†λŠ” 경우
            word_lst[word] = 1

word_lst = sorted(word_lst.items(), key = lambda x : (-x[1], -len(x[0]), x[0])) # x[0] = 단어, x[1] = 단어 λΉˆλ„μˆ˜

μ΄λ ‡κ²Œ 넣어놓은 λ‹€μŒ λ‘λ²ˆμ§Έ μˆœμ„œλ‘œ
λ”•μ…”λ„ˆλ¦¬μ— key의 len이 κΈ΄κ±° λ¨Όμ € μ •λ ¬λ˜κ²Œ 정렬을 ν•΄μ€¬μŠ΅λ‹ˆλ‹€!!

@alstjr7437 alstjr7437 self-requested a review March 16, 2024 09:42
Comment on lines +8 to +11
if word in word_count :
word_count[word][0] += 1
else :
word_count[word] = [1,len(word)]
Copy link
Member

Choose a reason for hiding this comment

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

μ €λŠ” μ •λ ¬ν•˜λŠ” 뢀뢄은 λ―Όμ„λ‹˜μ΄λž‘ λ˜‘κ°™μ€λ°μš”
λ”•μ…”λ„ˆλ¦¬μ—μ„œ get(value, μ—†μ„λ•Œμ‚¬μš©ν•  κ°’) 으둜 ν•˜λ©΄ 값이 λ“€μ–΄μžˆμ§€ μ•Šμ•„λ„ μ“Έ 수 μžˆλŠ”κ±Έλ‘œ
μ½”λ“œλ₯Ό μ’€ νŽΈν•˜κ²Œ μ“Έ κ±° κ°™μ•„μš”. ν™μ€€λ‹˜μ²˜λŸΌ κΈΈμ΄κΉŒμ§€ μ €μž₯ν•˜μ‹œλ©΄ 이 방법은 λͺ» μ”λ‹ˆλ‹€λ§Œ..

for _ in range(N):
    word = input().rstrip()
    if len(word) >= M :
        word_count[word] = word_count.get(word, 0) + 1

p.s νŒŒμ΄μ¬μ—μ„œ len ν•¨μˆ˜λ₯Ό μ“°λ©΄ μ‹œκ°„λ³΅μž‘λ„λŠ” μ–΄λ–»κ²Œ λ κΉŒμš”? O(1) μž…λ‹ˆλ‹€.
파이썬 κ°μ²΄λŠ” 길이가 λ³€ν•  λ•Œλ§ˆλ‹€ λ‚΄λΆ€μ˜ μΉ΄μš΄ν„°κ°€ μ¦κ°ν•˜λ―€λ‘œ 이미 μ €μž₯된 valueλ₯Ό κΊΌλ‚΄μ˜€λŠ” μž‘μ—…μ΄λΌ
길이λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜κ°€ λΉ λ₯΄κ²Œ λŒμ•„κ°‘λ‹ˆλ‹€!

@wkdghdwns199 wkdghdwns199 merged commit 392bcc8 into main Mar 27, 2024
11 checks passed
@wkdghdwns199 wkdghdwns199 deleted the 14-wkdghdwns199 branch March 27, 2024 05:41
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