[BOJ] 15829. Hashing(python)
๐๋ฌธ์
๐ช์์ด๋์ด
์ํ๋ฒณ ์๋ฌธ์๋ฅผ ์ซ์๋ก ๋ฐ๊พธ๊ธฐ
ord(โ์๋ฌธ์โ)๋ ์๋ฌธ์์ ASCII๊ฐ์ ๋ฐํํด์ค๋ค. ์๋ฌธ์์ ์์์ โaโ์ด๊ณ a๋ 1์ด๊ธฐ ๋๋ฌธ์- ord('a') + 1
ํด์ค๋ค.MOD๊ฐ
๐ฅ์ฝ๋
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.