Pagini recente » Cod sursa (job #2527423) | Cod sursa (job #60717) | Cod sursa (job #1505075) | Cod sursa (job #503035) | Cod sursa (job #1849626)
#include <fstream>
#include <stdint.h>
#include <vector>
#include <string>
#include <cmath>
const uint32_t hash_mod = 49157;
std::vector<uint32_t> hash[hash_mod];
std::string to_decode;
int32_t main()
{
std::ifstream fin("abc2.in");
std::ofstream fout("abc2.out");
uint32_t word_size = 0;
std::getline(fin, to_decode);
while (!fin.eof())
{
uint32_t hash_key = 0;
std::string c_word;
std::getline(fin, c_word);
word_size = c_word.size();
for (auto it : c_word) {hash_key *= 3; hash_key += (it - 'a');}
hash[hash_key % hash_mod].push_back(hash_key);
}
uint32_t solution = 0;
uint32_t current_key = 0;
for (uint32_t it = 0; it < word_size; ++it)
{
current_key *= 3;
current_key += (to_decode[it] - 'a');
}
for (auto it : hash[current_key % hash_mod]) {if (it == current_key) ++solution; break;}
for (uint32_t it = word_size; it < to_decode.size(); ++it)
{
current_key -= (pow(3, word_size - 1) * (to_decode[it - word_size] - 'a'));
current_key *= 3;
current_key += (to_decode[it] - 'a');
for (auto it : hash[current_key % hash_mod]) {if (it == current_key) ++solution; break;}
}
fout << solution;
fout.close();
fin.close();
return 0;
}