Cod sursa(job #2391605)

Utilizator S_AndyAndrei S S_Andy Data 29 martie 2019 03:56:26
Problema Potrivirea sirurilor Scor 38
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <vector>
#include <string>

using namespace std;

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

vector<int> matches;
string a, b;


bool Check_Match(int j) {
	for (int i = 0; i < a.size(); ++i) {
		if (a[i] != b[j++]) {
			return false;
		}
	}
	return true;
}

void Try_Match(int j) {
	if (Check_Match(j)) {
		matches.push_back(j);
	}
}

int main() {
	getline(fin, a);
	getline(fin, b);
	int n = b.size() - a.size() + 1;
	long long s = 0, z = 0;
	for (int i = 0; i < a.size(); ++i) {
		s += int(a[i]);
		z += int(b[i]);
	}
	for (int i = 1, j = a.size(); i < n; ++i) {
		z = z - int(b[i - 1]) + int(b[j++]);
		if (z == s) {
			Try_Match(i);
		}
	}
	fout << matches.size() << "\n";
	for (int i = 0; i < matches.size(); ++i) {
		fout << matches[i] << " ";
	}
}