Pagini recente » Cod sursa (job #2827055) | Cod sursa (job #1151461) | Cod sursa (job #2167139) | Cod sursa (job #538409) | Cod sursa (job #549454)
Cod sursa(job #549454)
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define FIN "strmatch.in"
#define FOUT "strmatch.out"
#define MAX 2000001
#define P1 100007
#define P2 100003
#define Size 63
int L, R1, R2;
char A[MAX], B[MAX], cod[256];
int H[MAX];
int main()
{
int i, x1, x2, y1, y2;
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
for (i = 0; i <= 9; ++ i)
cod[i + '0'] = i + 1;
for (i = 0; i < 26; ++ i)
cod[i + 'a'] = 11 + i;
for (i = 0; i < 26; ++ i)
cod[i + 'A'] = 37 + i;
scanf("%s %s", &B, &A);
L = strlen(B);
for (i = R1 = R2 = 1; i < L; ++ i)
{
R1 = (R1 * Size) % P1;
R2 = (R2 * Size) % P2;
}
for (i = x1 = x2 = y1 = y2 = 0; i < L; ++ i)
{
y1 = (y1 * Size + cod[B[i]]) % P1;
y2 = (y2 * Size + cod[B[i]]) % P2;
x1 = (x1 * Size + cod[A[i]]) % P1;
x2 = (x2 * Size + cod[A[i]]) % P2;
}
if (x1 == y1 && x2 == y2)
H[++ H[0]] = 0;
for (i = L; A[i]; ++ i)
{
x1 = (P1 + x1 - (cod[A[i - L]] * R1) % P1) % P1;
x2 = (P2 + x2 - (cod[A[i - L]] * R2) % P2) % P2;
x1 = (x1 * Size + cod[A[i]]) % P1;
x2 = (x2 * Size + cod[A[i]]) % P2;
if (i >= L - 1 && x1 == y1 && x2 == y2)
H[++ H[0]] = i - L + 1;
}
printf("%d\n", H[0]);
for (i = 1; i <= H[0] & i <= 1000; ++ i)
printf("%d ", H[i]);
printf("\n");
}