Pagini recente » Cod sursa (job #2988018) | Cod sursa (job #533670) | Cod sursa (job #434151) | Cod sursa (job #2507214) | Cod sursa (job #2795344)
#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] == T[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] << ' ';
}
return 0;
}