문제 링크입니다: https://www.acmicpc.net/problem/15829
15829번: Hashing
APC에 온 것을 환영한다. 만약 여러분이 학교에서 자료구조를 수강했다면 해시 함수에 대해 배웠을 것이다. 해시 함수란 임의의 길이의 입력을 받아서 고정된 길이의 출력을 내보내는 함수로 정�
www.acmicpc.net
이 문제를 풀기 위해서는 모듈러 연산의 속성에 대해 알고 있어야 했습니다.
모듈러 연산의 속성은 아래와 같습니다.
1. (a + b) mod n = {(a mod n) + (b mod n)} mod n
2. (a - b) mod n = {(a mod n) - (b mod n)} mod n
3. (a * b) mod n = {(a mod n) * (b mod n)} mod n
해당 문제에서 쓰이는 속성은 1번과 3번 속성이고 주어진 수식에 그대로 적용하면 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
const int MOD = 1234567891; | |
const int MULTIPLY = 31; | |
int main(void) | |
{ | |
int L; | |
cin >> L; | |
string s; | |
cin >> s; | |
long long sum = 0; | |
long long R = 1; | |
for (int i = 0; i < s.length(); i++) | |
{ | |
sum = (sum + (s[i] - 'a' + 1) * R) % MOD; | |
R = (R*MULTIPLY) % MOD; | |
} | |
cout << sum << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 13904번 과제 (0) | 2020.05.21 |
---|---|
백준 1949번 우수 마을 (0) | 2020.05.18 |
백준 16500번 문자열 판별 (0) | 2020.05.14 |
백준 1059번 수2 (0) | 2020.05.08 |
백준 2592번 대표값 (0) | 2020.05.03 |