Pagini recente » Cod sursa (job #333047) | Cod sursa (job #1530) | Cod sursa (job #1079288) | Cod sursa (job #450159) | Cod sursa (job #3203058)
#include <bits/stdc++.h>
#define MAXSZ 2000005
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
int n, m, lps[MAXSZ];
char a[MAXSZ], b[MAXSZ];
int main() {
fin.getline(a, MAXSZ);
fin.getline(b, MAXSZ);
n = (int) strlen(a);
m = (int) strlen(b);
int idx = 0;
for (int i = 2; i <= n; i++) {
while (idx && a[i - 1] != a[idx])
idx = lps[idx];
if (a[i - 1] == a[idx])
idx++;
lps[i] = idx;
}
idx = 0;
vector<int> matches;
for (int i = 1; i <= m; i++) {
while (idx && a[idx] != b[i - 1])
idx = lps[idx];
if (a[idx] == b[i - 1])
idx++;
if (idx == n) {
matches.push_back(i - n);
idx = lps[idx];
}
}
fout << matches.size() << '\n';
for (int i = 0; i < min((int) matches.size(), 1000); i++)
fout << matches[i] << ' ';
return 0;
}