Cod sursa(job #2595769)

Utilizator k2e0e0w3qDumitrescu Gheorghe k2e0e0w3q Data 8 aprilie 2020 13:22:31
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>

char t[2000001], s[2000001];
int pi[2000001];
void compute () {
	int i=1, len=0;
	while (t[i])
		if (t[i]==t[len])
			pi[i++]=++len;
		else
			if (len)
				len=pi[len-1];
			else
				pi[i++]=0;
}

int main (void) {
    std::ifstream fin ("strmatch.in");
    std::ofstream fout ("strmatch.out");
    fin >> t >> s;

    fout << "                                          \n";
	compute();
	int i=0, j=0, ct=0;
	while (s[i] && ct!=1000)
		if (s[i]==t[j]) {
			++i, ++j;
            if (!t[j]) {
                ++ct;
                fout << i-j << ' ';
                j=pi[j-1];
            }
        }
		else
            if (j)
                j=pi[j-1];
            else
                ++i;
	fout.seekp(0);
	fout << ct;
	return 0;
}