[백준] 15829번 : Hashing

letsbebrave·2022년 3월 17일
0

codingtest

목록 보기
51/146

문제

https://www.acmicpc.net/problem/15829

개념

아스키 코드(숫자) -> 문자 : chr(n)

print(chr(65)) # A
print(chr(97)) # a

문자 -> 아스키 코드(숫자) : ord('A')

print(ord('A')) # 65
print(ord('a')) # 97

풀이

100점 풀이

from sys import stdin

L = int(stdin.readline())
word = stdin.readline().strip()
M = 1234567891

result = 0
for i in range(len(word)):
    num =  ord(word[i])-96
    result += 31**i * num
print(result % M)

50점 풀이

from sys import stdin
import math

L = int(stdin.readline())
word = stdin.readline().strip()
dic = {
    'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 
    'f':6, 'g':7, 'h':8, 'i':9, 'j':10,
    'k':11, 'l':12, 'm':13, 'n':14,'o':15,
    'p':16,'q':17,'r':18, 's':19,'t':20,
    'u':21, 'v':22, 'w':23,'x':24,'y':25, 'z':26
}

result = 0
count = 0
for i in word:
    result += math.pow(31,count) * dic[i]
    count += 1

print(int(result % 1234567891))
profile
그게, 할 수 있다고 믿어야 해

0개의 댓글