Pagini recente » Cod sursa (job #2107439) | Rating Anghel Victor Valentin (veve07) | Cod sursa (job #630295) | Cod sursa (job #1451233) | Cod sursa (job #2352741)
#include <bits/stdc++.h>
#define MAXN 2000005
int M, N;
char B[MAXN], A[MAXN];
int LPS[MAXN];
void ComputeLPS() {
int Len = 0;
for (int i=2; i<=N; ++i) {
while (Len && A[Len+1] != A[i])
Len = LPS[Len];
if (A[Len+1] == A[i])
++ Len;
LPS[i] = Len;
}
}
int Count;
std::vector <int> Ans;
void KMP() {
ComputeLPS();
int Len = 0;
for (int i=1; i<=M; ++i) {
while (Len && B[i] != A[Len+1])
Len = LPS[Len];
if (A[Len+1] == B[i])
++Len;
if (Len == N) {
++ Count;
if (Ans.size() <= 1000)
Ans.push_back(i-N);
}
}
}
std::ifstream In ("strmatch.in");
std::ofstream Out("strmatch.out");
void Citire() {
In >> A+1 >> B+1;
M = strlen(B+1);
N = strlen(A+1);
}
void Rezolvare() {
KMP();
Out << Count << '\n';
for (auto idx:Ans)
Out << idx << ' ';
}
int main()
{
Citire();
Rezolvare();
return 0;
}