Cod sursa(job #2604234)

Utilizator lepoartcevPaltineanu Rares-Mihai lepoartcev Data 22 aprilie 2020 10:48:27
Problema Potrivirea sirurilor Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 3.02 kb
/*#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define mod 10000000
#define mod2 10000009
#define p 256
int main()
{
    FILE* in = fopen("strmatch.in", "r");
    FILE* out = fopen("strmatch.out", "w");
    char* a = (char*)malloc(2000001);
    char* b = (char*)malloc(2000001);

    fgets(a, 2000001, in);
    fgets(b, 2000001, in);
    int size1 = strlen(a);
    int size2 = strlen(b);

    int hash1 = 0, hash2 = 0;
    int p1 = 1, p2 = 1;

    for(int i = 0; i < size1; i++) {

        hash1 = (hash1 * p + a[i]) % mod;
        hash2 = (hash2 * p + a[i]) % mod2;

        if(i != 0) {

            p1 = p1 * p % mod;
            p2 = p2 * p % mod2;

        }
    }

    if(size1 > size2) {

        fprintf(out, "0\n");
        return 0;

    }

    int hash3 = 0;
    int hash4 = 0;
    int nr = 0;

    for(int i = 0; i < size1; i++) {

        hash3 = (hash3 * p + b[i]) % mod;
        hash4 = (hash4 * p + b[i]) % mod2;

    }

    int pos[100000];
    memset(pos, 0, sizeof(int) * 100000);
    if(hash3 == hash1 && hash4 == hash2) {

        pos[nr] = 1;
        nr++;

    }

    for(int i = size1; i < size2; i++) {

        hash3 = ((hash3 - (b[i - size1] * p1) % mod + mod) * p + b[i]) % mod;
		hash4 = ((hash4 - (b[i - size1] * p2) % mod2 + mod2) * p + b[i]) % mod2;



		if (hash3 == hash1 && hash4 == hash2)

			pos[ i - size1 + 1 ] = 1, nr++;


    }

    fprintf(out, "%d\n", nr);
    nr = 0;
    for(int i = 0; i < size2 && nr < 1000; i++)
    if(pos[i]){

        nr++;
        fprintf(out, "%d ", i);

    }
    return 0;
}
*/
#include <stdio.h>

#include <string.h>



#define MAXN 2000001



#define P 73

#define MOD1 100007

#define MOD2 100021



char A[MAXN], B[MAXN];

int NA, NB;



int hashA1, hashA2, P1, 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]) % MOD1;

		hashA2 = (hashA2 * P + A[i]) % MOD2;



		if (i != 0)

			P1 = (P1 * P) % MOD1,

			P2 = (P2 * P) % MOD2;

	}



	if (NA > NB)

	{

		printf("0\n");

		return 0;

	}



	int hash1 = 0, hash2 = 0;

	for (int i = 0; i < NA; i++)

		hash1 = (hash1 * P + B[i]) % MOD1,

		hash2 = (hash2 * P + B[i]) % MOD2;



	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) % MOD1 + MOD1) * P + B[i]) % MOD1;

		hash2 = ((hash2 - (B[i - NA] * P2) % MOD2 + MOD2) * P + B[i]) % MOD2;



		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;

}