Pagini recente » Cod sursa (job #930876) | Cod sursa (job #376261) | Cod sursa (job #3148724) | Cod sursa (job #2105489) | Cod sursa (job #2983882)
#include <bits/stdc++.h>
#define MAXSZ 2000005
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
char a[MAXSZ], b[MAXSZ];
int lps[MAXSZ];
int main() {
fin.getline(a + 1, MAXSZ);
fin.getline(b + 1, MAXSZ);
int aSz = (int) strlen(a + 1), bSz = (int) strlen(b + 1);
int length = 0;
for (int i = 2; i <= aSz; i++) {
while (length > 0 && a[length + 1] != a[i])
length = lps[length];
if (a[length + 1] == a[i])
length++;
lps[i] = length;
}
vector<int> matches;
length = 0;
for (int i = 1; i <= bSz; i++) {
while (length > 0 && a[length + 1] != b[i])
length = lps[length];
if (a[length + 1] == b[i])
length++;
if (length == aSz) {
matches.push_back(i - aSz);
length = lps[length];
}
}
fout << matches.size() << '\n';
for (auto& match: matches)
fout << match << ' ';
return 0;
}