blob: ee7f57f0e3ff76e250f4b101abed44b9dfeaf417 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#include <iostream>
unsigned fnvHash(const char* str) {
unsigned hash = 2166136261;
while (*str != '\0') {
hash *= 16777619;
hash ^= *str;
str++;
}
return hash;
}
int main() {
unsigned allocated = 8, size = 0;
char* sentence = new char[allocated];
char curr;
std::cin.get(curr);
while (curr != '\n') {
sentence[size++] = curr;
std::cin.get(curr);
// Реоразмеряване
if (size >= allocated) {
allocated *= 2;
char* newSentenceArray = new char[allocated];
for (unsigned i = 0; i < size; i++)
newSentenceArray[i] = sentence[i];
delete[] sentence;
sentence = newSentenceArray;
}
}
sentence[size] = '\0';
std::cout << fnvHash(sentence) << std::endl;
delete[] sentence;
return 0;
}
|