Pagini recente » Cod sursa (job #625071) | Cod sursa (job #2196755) | Cod sursa (job #2221118) | Cod sursa (job #38613) | Cod sursa (job #1896075)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#define NMAX 2000005
using namespace std;
ifstream fin ("strmatch.in");
ofstream fout ("strmatch.out");
char a[NMAX], b[NMAX], n, m;
int pi[NMAX];
vector <int> matches;
void kmp () {
int k = 0;
for (int i = 2; i <= n; ++i) {
while (k != 0 && a[k+1] != a[i]) {
k = pi[k];
}
if (a[k+1] == a[i]) {
++k;
}
pi[i] = k;
}
}
void match() {
int k = 0;
for (int i = 2; i <= m; ++i) {
while (k != 0 && a[k+1] != b[i]) {
k = pi[k];
}
if (a[k+1] == b[i]) {
++k;
}
if (k == n) {
matches.push_back(i - n);
k = pi[k];
}
}
}
int main ()
{
fin.getline(a + 1,NMAX);
n = strlen(a + 1);
fin.getline(b + 1,NMAX);
m = strlen(b + 1);
kmp();
/*for (int i = 0; i <= n; ++i) {
cout << pi[i] << " ";
}*/
match();
fout << matches.size() << '\n';
for (int i = 0; i < matches.size(); ++i) {
fout << matches[i] << " ";
}
fin.close();
fout.close();
return 0;
}