Pagini recente » Cod sursa (job #2654088) | Cod sursa (job #2905603) | Cod sursa (job #2376501) | Cod sursa (job #1398311) | Cod sursa (job #1373035)
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;
ifstream f ("strmatch.in");
ofstream g ("strmatch.out");
const int B = 127, LMAX = 2000000 + 1;
char text[LMAX], pattern[LMAX];
int P[LMAX];
vector <int> sol;
void citeste() {
f >> pattern >> text;
}
void build() {
P[0] = 0;
int k = 0, n = strlen(pattern);
for (int i = 1; i < n; i++) {
while (k != 0 && pattern[i] != pattern[k]) k = P[k - 1];
if (pattern[i] == pattern[k]) k++;
P[i] = k;
}
}
void KMP() {
build();
int k = 0;
int n = strlen(text), m = strlen(pattern);
for (int i = 0; i < n; i++) {
while (k != 0 && text[i] != pattern[k]) k = P[k - 1];
if (pattern[k] == text[i]) k++;
if (k == m) sol.push_back(i - m + 1);
}
}
void scrie() {
int l = sol.size();
g << l << '\n';
l = min(l, 1000);
for (int i = 0; i < l; i++) g << sol[i] << ' ';
}
int main() {
citeste();
KMP();
scrie();
return 0;
}