Cod sursa(job #2550614)

Utilizator nicuvladNicu Vlad nicuvlad Data 18 februarie 2020 21:53:44
Problema Potrivirea sirurilor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include<bits/stdc++.h>
#define MAX 2000005
using namespace std;

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

int m, n, k, c, urm[MAX];
char P[MAX], T[MAX];	///P- pattern T -sir
int a[MAX];
void Prefix()
{
	int j = 0;
	for (int i = 1; i < n; ++i)
	{
		while (j > 0 && P[i] != P[j])
			j = urm[j - 1];
		if (P[i] == P[j])
			j++;
		urm[i] = j;
	}
}

void KMP()
{
	int q = 0;
	for (int i = 0; i < m; ++i)
	{
		while (q > 0 && P[q] != T[i])
			q = urm[q - 1];
		if (P[q] == S[i])  ++q; ///s-au potrivit
		if (q == n)
		{
			a[c++] = i - n + 1;
		}
	}
}

void Citire()
{
	fin.getline(P, MAX);
	fin.getline(T, MAX);
	n = strlen(P);
	m = strlen(T);
}

int main()
{
	Citire();
	Prefix();
	KMP();
	fout << c << '\n';
	if (c > 1000)
		c = 1000;
	for (int i = 0; i < c; ++i)
	{
		fout << a[i] << ' ';
	}
}