Pagini recente » Cod sursa (job #1596482) | Cod sursa (job #2032830) | Cod sursa (job #597637) | Cod sursa (job #168354) | Cod sursa (job #1522258)
#include <cstdio>
#define TEXT_MAX 2000005
#define NMAX 1001
using namespace std;
char A[TEXT_MAX], B[TEXT_MAX];
int pos[TEXT_MAX], N, res[NMAX];
void computePos() {
int index = 0;
pos[index] = -1;
while (A[++index] != '\0') {
int aux = pos[index - 1];
while (aux != -1 && A[index] != A[aux + 1]) {
aux = pos[aux];
}
if (A[index] == A[aux + 1]) {
aux++;
}
pos[index] = aux;
}
}
void readInput() {
freopen("strmatch.in", "r", stdin);
freopen("strmatch.out", "w", stdout);
scanf("%s\n", A);
scanf("%s", B);
}
void match() {
int bIndex = 0;
int aIndex = 0;
while (B[bIndex] != '\0') {
while (A[aIndex] != '\0' && B[bIndex] != '\0' && A[aIndex] == B[bIndex]) {
aIndex++;
bIndex++;
}
if (aIndex == 0) {
bIndex++;
continue;
}
if (A[aIndex] == '\0') {
N++;
if (N < NMAX) {
res[N] = bIndex - aIndex;
}
}
aIndex = pos[aIndex - 1] + 1;
}
}
void printSol() {
printf("%d\n", N);
N = N > 1000 ? 1000 : N;
for (int i = 1; i <= N; i++) {
printf("%d ", res[i]);
}
}
int main() {
readInput();
computePos();
match();
printSol();
return 0;
}