Post

[BOJ] 15829. Hashing(python)

๐Ÿ“Œ๋ฌธ์ œ

Alt text

๐Ÿ’ช์•„์ด๋””์–ด

  1. ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๊ธฐ
    ord(โ€˜์†Œ๋ฌธ์žโ€™)๋Š” ์†Œ๋ฌธ์ž์˜ ASCII๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. ์†Œ๋ฌธ์ž์˜ ์‹œ์ž‘์€ โ€˜aโ€™์ด๊ณ  a๋Š” 1์ด๊ธฐ ๋•Œ๋ฌธ์— - ord('a') + 1 ํ•ด์ค€๋‹ค.

  2. MOD๊ฐ’

$ (A+B)\ MOD \ C \ = \ (A \ MOD \ C \ + \ B \ MOD \ C) \ MOD \ C $

๐Ÿฅ‚์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
import sys
input = sys.stdin.readline

l = int(input())
s = input().rstrip()
val = 0

for i in range(l):
    alphaNum = ord(s[i])-ord('a')+1
    val += (alphaNum * 31**i)%1234567891

print(val%1234567891)
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.