Pagini recente » Cod sursa (job #1561949) | Cod sursa (job #244219) | Cod sursa (job #2586449) | Arhiva de probleme | Cod sursa (job #2976774)
#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 + 1], b[MAXSZ + 1];
int main() {
fin.getline(a, MAXSZ);
fin.getline(b, MAXSZ);
n = (int) strlen(a);
m = (int) strlen(b);
int q = 0;
for (int i = 2; i <= n; i++) {
while (q && a[q] != a[i - 1])
q = lps[q];
if (a[q] == a[i - 1])
q++;
lps[i] = q;
}
vector<int> matches;
q = 0;
for (int i = 0; i < m; i++) {
while (q && a[q] != b[i])
q = lps[q];
if (a[q] == b[i])
q++;
if (q == n) {
matches.push_back(i - n + 1);
q = lps[q];
}
}
fout << matches.size() << '\n';
for (int i = 0; i < min((int) matches.size(), 1000); i++)
fout << matches[i] << ' ';
return 0;
}