λ°μν
π€λ¬Έμ ν΄κ²°
- λμ
λ리μ νΌμ¦μ κ° μνλ²³μ ν€, κ·Έ μνλ²³μ κ°μμ μ¬μ©λ νμμ λμ
λ리λ₯Ό 벨λ₯λ‘ κ°μ§λ€.
- νΌμ¦μ μλ μνλ²³μΌλ‘ λ±λ§μ λ§λ€ μ μλ€λ©΄ νΌμ¦μμ μ¬μ©λ μνλ²³μ used_cnt λ₯Ό +1 ν΄μ€λ€.
- μμ κ°μ κ²°κ³Όκ° λμ€κ² λκ³ , used_cnt κ° κ°μ₯ μ μ κ°λ€, κ°μ₯ λ§μ κ°λ€μ μνλ²³ μ€λ¦μ°¨μμΌλ‘ μΆλ ₯ν΄μ€λ€.
π»μμ€ μ½λ
import pprint
import sys
input = sys.stdin.readline
# μΈν λ°μμ λ¨μ΄ or νΌμ¦ λ°λ‘ λ°°μ΄λ‘ λ§λλ ν¨μ (λλ² λ°λ³΅ν κ±° κ°μμ ν¨μλ‘ λ§λ¦)
def input_func(arr, sign):
arr = []
while True:
input_value = input().strip()
if input_value == sign:
break
arr.append(input_value)
return arr
words = input_func([], '-')
puzzles = input_func([], '#')
# print(words)
# print(puzzles)
for puzzle in puzzles:
# print()
puzzle_dict = {} # κ³μ°ν κ²°κ³Όλ₯Ό μμ dict
for p in puzzle: # νΌμ¦μμ λ¨μ΄ νλμ© cnt: λ¨μ΄ κ°―μ, used_cnt: μ¬μ©λ μ μλ wordμ λν΄μ μ¬μ©λλ κ°μ
if not puzzle_dict.get(p):
puzzle_dict[p] = {'cnt': 1, 'used_cnt': 0}
else:
puzzle_dict[p]['cnt'] += 1
# pprint.pprint(puzzle_dict)
for word in words:
for w in set(word):
w_cnt = word.count(w)
# print(w_cnt)
if not puzzle_dict.get(w) or w_cnt > puzzle_dict[w]['cnt']: # μνλ²³μ΄ νΌμ¦μ μκ±°λ, νΌμ¦μ μλ μνλ²³ κ°μλ³΄λ€ λ λ§μΌλ©΄ μ¬μ© λͺ»ν¨
break
else: # μ¬μ©λ μ μλ wordλ κ° μνλ²³μ used_cnt λ₯Ό +1 ν΄μ€λ€.
for w in set(word):
puzzle_dict[w]['used_cnt'] += 1
# pprint.pprint(puzzle_dict)
# λ΅ κ΅¬νκΈ°
min_cnt, max_cnt = 1e9, 0
min_words, max_words = [], []
for key, val in puzzle_dict.items():
if val['used_cnt'] < min_cnt:
min_words = [key]
min_cnt = val['used_cnt']
elif val['used_cnt'] == min_cnt:
min_words.append(key)
if val['used_cnt'] > max_cnt:
max_words = [key]
max_cnt = val['used_cnt']
elif val['used_cnt'] == max_cnt:
max_words.append(key)
# print(min_words, min_cnt)
# print(max_words, max_cnt)
print(''.join(sorted(min_words)), min_cnt, ''.join(sorted(max_words)), max_cnt)
πλ¬Έμ νμΈ
μΆμ²: BACKJOON ONLINE JUDGE
λ°μν
'Algorithm Problem > Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[python] λ°±μ€ - 1013. Contact (0) | 2021.09.08 |
---|---|
[python] λ°±μ€ - 1174. μ€μ΄λλ μ«μ (0) | 2021.09.07 |
[python] λ°±μ€ - 1105. ν (0) | 2021.09.05 |
[python] λ°±μ€ - 13023. ABCDE (0) | 2021.09.04 |
[python] λ°±μ€ - 2615. μ€λͺ© (2) | 2021.09.03 |