백준 15829번: Hashing #Python

ColorlessDia·2025년 5월 8일

algorithm/baekjoon

목록 보기
536/807
L = int(input())
S = input()

M = 1234567891

lowercase = [chr(i) for i in range(ord('a'), ord('z') + 1)]
matched_hash = dict(zip(lowercase, range(1, 26 + 1)))

hash_value = 0

for i in range(L):
    H = matched_hash[S[i]] * (31 ** i)

    hash_value += H

print(hash_value % M)

0개의 댓글