Cod sursa(job #820527)

Utilizator UnforgivenMihai Catalin Botezatu Unforgiven Data 20 noiembrie 2012 23:27:04
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <vector>
#define MAX_SIZE 2000001
#define term 73
#define MOD1 100013
#define MOD2 100007

using namespace std;

char A[MAX_SIZE],B[MAX_SIZE];
int hash1 , hash2 = 0;
int hash11 , hash22 = 0 ;

vector <int> gasit;
int main()
{
    freopen("strmatch.in","r",stdin);
    freopen("strmatch.out","w",stdout);

    scanf("%s",A);
    scanf("%s",B);
    int NA = strlen(A);
    int NB = strlen(B);
    int P1 = 1;
    int P2 = 1;
    for (int i=0;i<NA;i++)
    {
        hash1 = (hash1 * term + A[i]) % MOD1;
        hash11 = (hash11 * term + A[i]) % MOD2;
        if (i != 0)
        {
            P1 = (P1 * term) % MOD1;
            P2 = (P2 * term) % MOD2;
        }
    }
    if (NA > NB)
     {
         printf("0");
         return 0;
     }
    for (int i=0;i<NA;i++)
    {
        hash2 = (hash2 * term + B[i]) % MOD1;
        hash22 = (hash22 * term + B[i]) % MOD2;
    }
    int raspuns = 0;
    if (hash1 == hash2 && hash11 == hash22)
    {
        raspuns ++;
        gasit.push_back(0);
    }
    for (int i=NA;i<NB;i++)
    {
        hash2 = ((hash2 - (B[i - NA] * P1)% MOD1 + MOD1 ) * term  + B[i]) % MOD1;
        hash22 = ((hash22 - (B[i - NA] * P2) % MOD2 + MOD2) * term  + B[i]) % MOD2;
        if (hash2 == hash1 && hash11 == hash22)
        {
            raspuns++;
            gasit.push_back(i-NA+1);
        }
    }

    printf("%d\n",raspuns);
    for (int i=0;i<gasit.size();i++)
    {
        printf("%d ",gasit[i]);
    }
    return 0;
}