Cod sursa(job #2488040)

Utilizator FlorianMarcuMarcu Florian Cristian FlorianMarcu Data 6 noiembrie 2019 00:27:50
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
// PotrivireaSirurilor.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <fstream>
#include <string>

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

int main()
{
	std::string A, B;
	int* v = new int[1000];
	int hash = 0;
	int sum = 0;
	int app = 0;
	fin >> A >> B;
	int m = A.length(), n = B.length();
	for (int i = 0 ; i < m; i++)
	{
		hash += int(A[i]);
		sum += int(B[i]);
	}
	
	for (int i = m - 1; i < n; i++) {
		if (i >= m)
			sum += int(B[i]);
		if (sum == hash)
			if(B.substr(i - m + 1, m) == A)
			v[app++] = i - m + 1;
		sum -= int(B[i - m + 1]);
	}
	if (app != 0) {
		fout << app << std::endl;
		for (int i = 0; i < app; i++)
			fout << v[i] << " ";
	}
	else fout << 0;

}