Cod sursa(job #820521)

Utilizator UnforgivenMihai Catalin Botezatu Unforgiven Data 20 noiembrie 2012 23:08:06
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <vector>
#define MAX_SIZE 2000000
#define term 123
using namespace std;

char A[MAX_SIZE],B[MAX_SIZE];
unsigned int hash1 , hash2 = 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 P = 1;
    for (int i=0;i<NA;i++)
    {
        hash1 = hash1 * term + A[i];
        if (i != 0)
        P *= term;
    }
    for (int i=0;i<NA;i++)
    {
        hash2 = hash2 * term + B[i];

    }
    int raspuns = 0;
    if (hash1 == hash2)
    {
        raspuns += 1;
        gasit.push_back(0);
    }
    for (int i=NA;i<NB;i++)
    {
        hash2 = ((hash2 - (B[i - NA] * P)) * term ) + B[i];
        if (hash2 == hash1)
        {
            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;
}