Cod sursa(job #2378205)

Utilizator puzzleFlutur Vasile puzzle Data 11 martie 2019 20:55:41
Problema Potrivirea sirurilor Scor 26
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.7 kb
#include <fstream>
#include <string>
#include <deque>

std::ifstream in("strmatch.in");
std::ofstream out("strmatch.out");

const int ch = 256; //no characters

void PrintSolutions(std::deque<int>& Solutions)
{
	out << Solutions.size() << '\n';
	while (!Solutions.empty())
	{
		out << Solutions.front() << " ";
		Solutions.pop_front();
	}
}

void Rabin_Karp(std::string& pattern, std::string& text, int primeN)
{
	int i, j;

	std::deque<int> answers;

	int PatternHash = 0;
	int TextHash = 0;

	int ReHash = 1; 
	for (int i = 0; i < pattern.size() - 1; i++)
		ReHash = (ReHash * ch) % primeN;
	//ReHash is used when we update the hash window value

	//Calculating the hash value of Pattern and Text
	for (i = 0; i < pattern.size(); i++)
	{
		PatternHash = (ch * PatternHash + pattern[i]) % primeN;
		TextHash = (ch * TextHash + text[i]) % primeN;
	}

	for (i = 0; i <= text.size() - pattern.size(); i++)
	{
		if (PatternHash == TextHash)
		{
			for (j = 0; j < pattern.size(); j++)
			{
				if (pattern[j] != text[i + j])
					break;
			}
			if (j == pattern.size())
			{
				answers.push_back(i);
			}

		}
		//Calculate hash value for next window of text:
		//Remove leading digit
		//Add trailing digit
		if (i < text.size() - pattern.size())
		{
			TextHash = (ch * (TextHash - text[i] * ReHash) + text[i + pattern.size()]) % primeN;

			//We might get negative values for TextHash
			//Convert to positive

			if (TextHash < 0)
				TextHash += primeN;
		}
	}
	PrintSolutions(answers);
}

int main()
{
	std::string pattern;
	in >> pattern;
	
	std::string text;
	in >> text;

	int primeN = 101; //any prime number, key
	Rabin_Karp(pattern, text, primeN);
}