Cod sursa(job #1539138)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 30 noiembrie 2015 12:23:51
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main(){
	ifstream f("abc2.in");
	ofstream g("abc2.out");
	string str;
	f >> str;
	unordered_set<uint32_t> words;
	int len = 0;
	{
		string word;
		while(f >> word){
			uint32_t cod = 0;
			for(const auto x : word){
				cod = cod * 3 + x - 'a'; }
			words.insert(cod); }
		len = word.size(); }

	uint32_t pow_of_3 = 1;
	for(int i = 0; i < len-1; ++i){
		pow_of_3 *= 3; }

	uint32_t cod = 0;
	for(int i = 0; i < len; ++i){
		cod = cod * 3 + str[i] - 'a'; }

	int rez = (words.find(cod) != end(words));

	for(int st = 0, dr = len; dr < str.size(); ++st, ++dr){
		cod -= pow_of_3 * (str[st]-'a');

		cod = 3 * cod + str[dr] - 'a';
		rez += (words.find(cod) != end(words)); }

	g << rez;

	return 0; }