-
Notifications
You must be signed in to change notification settings - Fork 1
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
14-wkdghdwns199 #59
Conversation
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()
}
|
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)) |
There was a problem hiding this comment.
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μ΄ κΈ΄κ±° λ¨Όμ μ λ ¬λκ² μ λ ¬μ ν΄μ€¬μ΅λλ€!!
if word in word_count : | ||
word_count[word][0] += 1 | ||
else : | ||
word_count[word] = [1,len(word)] |
There was a problem hiding this comment.
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λ₯Ό κΊΌλ΄μ€λ μμ
μ΄λΌ
κΈΈμ΄λ₯Ό λ°ννλ ν¨μκ° λΉ λ₯΄κ² λμκ°λλ€!
π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/20920
λ¬Έμ
νμμ΄λ μ΄λ² μμ΄ μνμμ νλ¦° λ¬Έμ λ₯Ό λ°νμΌλ‘ μμ΄ λ¨μ΄ μκΈ°λ₯Ό νλ €κ³ νλ€. κ·Έ κ³Όμ μμ ν¨μ¨μ μΌλ‘ μμ΄ λ¨μ΄λ₯Ό μΈμ°κΈ° μν΄ μμ΄ λ¨μ΄μ₯μ λ§λ€λ € νκ³ μλ€. νμμ΄κ° λ§λ€κ³ μ νλ λ¨μ΄μ₯μ λ¨μ΄ μμλ λ€μκ³Ό κ°μ μ°μ μμλ₯Ό μ°¨λ‘λ‘ μ μ©νμ¬ λ§λ€μ΄μ§λ€.
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 μλ£νμ μ°μ μμ μ λ ¬μ νμΈν΄ λ³Ό μ μμλ€..!
β¨ μλ μ½λ
κ²°κ΅ λ€μ΄μ¨ λ¨μ΄λ€μ λ€μκ³Ό κ°μ 쑰건λ€μ ν΅ν΄ μ λ ₯λ°μ λ¨μ΄λ€μ μ λ ¬νλΌλ λ¬Έμ μμ΅λλ€.
listλ₯Ό λ€ λ°λ‘ λ°λ‘ μ λ ¬ν΄μ ν΄μΌ ν κΉ λΌλ μκ°μ λ§λ§νμ§λ§, dictionary λ‘ μμ£Ό λμ€λ λ¨μ΄λ₯Ό key κ°μΌλ‘ μΌμ sorted ν¨μλ‘ μ°μ μμ μ λ ¬μ ν μ μμ§ μμκΉλΌλ λ§μ°ν μκ°μΌλ‘ λ€μκ³Ό κ°μ μ½λλ₯Ό μ§°μ΅λλ€.
λλλΌκ΅¬μ! μ λ²μ νμ λμ΄ μ¬λ¦¬μ PR μμ λ§μνμ 쑰건 μ λ ¬ λ°©μμ μμ©ν΄μ λ€μ€ 쑰건μ κ±Έ μλ μλ€λ κ²μ μ μ μλ λ¬Έμ μμ΅λλ€!
π μλ‘κ² μκ²λ λ΄μ©