Pagini recente » Cod sursa (job #350298) | Cod sursa (job #860209) | Cod sursa (job #213785) | Cod sursa (job #799945) | Cod sursa (job #524410)
Cod sursa(job #524410)
#include <stdio.h>
#include <string.h>
#define MAXN 2000001
#define P 73
char A[MAXN], B[MAXN];
int NA, NB;
int hashA1, P1;
short int hashA2, P2;
char match[MAXN];
int main()
{
freopen("strmatch.in", "rt", stdin);
freopen("strmatch.out", "wt", stdout);
scanf("%s %s", A, B);
NA = strlen(A);
NB = strlen(B);
P1 = P2 = 1;
hashA1 = hashA2 = 0;
for (int i = 0; i < NA; i++)
{
hashA1 = (hashA1 * P + A[i]);
hashA2 = (hashA2 * P + A[i]);
if (i != 0)
P1 = (P1 * P),
P2 = (P2 * P);
}
if (NA > NB)
{
printf("0\n");
return 0;
}
int hash1 = 0;
short int hash2 = 0;
for (int i = 0; i < NA; i++)
hash1 = (hash1 * P + B[i]),
hash2 = (hash2 * P + B[i]);
int Nr = 0;
if (hash1 == hashA1 && hash2 == hashA2)
match[0] = 1, Nr++;
for (int i = NA; i < NB; i++)
{
hash1 = ((hash1 - (B[i - NA] * P1)) * P + B[i]);
hash2 = ((hash2 - (B[i - NA] * P2)) * P + B[i]);
if (hash1 == hashA1 && hash2 == hashA2)
match[ i - NA + 1 ] = 1, Nr++;
}
printf("%d\n", Nr);
Nr = 0;
for (int i = 0; i < NB && Nr < 1000; i++)
if (match[i])
Nr++,
printf("%d ", i);
printf("\n");
return 0;
}