Pagini recente » Cod sursa (job #2843933) | Cod sursa (job #1121619) | Cod sursa (job #1919324) | Cod sursa (job #1360163) | Cod sursa (job #2989948)
#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, MAXSZ);
fin.getline(b, MAXSZ);
int aSz = (int)strlen(a);
int bSz = (int)strlen(b);
for (int i = aSz; i > 0; i--)
a[i] = a[i - 1];
for (int i = bSz; i > 0; i--)
b[i] = b[i - 1];
int count = 0;
for (int i = 2; i <= aSz; i++) {
while (count > 0 && a[i] != a[count + 1])
count = lps[count];
if (a[i] == a[count + 1])
count++;
lps[i] = count;
}
vector<int> solutions;
count = 0;
for (int i = 1; i <= bSz; i++) {
while (count > 0 && b[i] != a[count + 1])
count = lps[count];
if (b[i] == a[count + 1])
count++;
if (count == aSz) {
solutions.push_back(i - aSz);
count = lps[count];
}
}
fout << solutions.size() << '\n';
for (auto& solution: solutions)
fout << solution << ' ';
return 0;
}