Cod sursa(job #483982)

Utilizator DraStiKDragos Oprica DraStiK Data 11 septembrie 2010 11:17:29
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <algorithm>
#include <vector>
using namespace std;

#define pb push_back
#define MAX 10000005
#define MOD 99987
#define LIM 25
#define P 3

vector <unsigned> h[MOD];
char s[MAX],buff[LIM];
int n,m,nrt;

inline int find (unsigned val,int mod)
{
    vector <unsigned> :: iterator it;

    for (it=h[mod].begin (); it!=h[mod].end (); ++it)
        if (*it==val)
            return 1;
    return 0;
}

inline void insert (unsigned val,int mod)
{
    if (!find (val,mod))
        h[mod].pb (val);
}

void read ()
{
    unsigned nr;
    int i;

    fgets (s,MAX,stdin);
    n=strlen (s)-1;
    for (nr=0; fgets (buff,LIM,stdin); nr=0)
    {
        m=strlen (buff)-1;
        for (i=0; i<m; ++i)
            nr=nr*P+buff[i]-'a';
        insert (nr,(int)(nr%MOD));
    }
}

void solve ()
{
    unsigned nr,put;
    int i;

    nr=0; put=1;
    for (i=0; i<m; ++i)
    {
        nr=nr*P+s[i]-'a';
        if (i)
            put*=P;
    }
    for (i=m; i<n; ++i)
    {
        nrt+=find (nr,(int)(nr%MOD));
        nr=(nr-(s[i-m]-'a')*put)*P+(s[i]-'a');
    }
    printf ("%d",nrt);
}

int main ()
{
    freopen ("abc2.in","r",stdin);
    freopen ("abc2.out","w",stdout);

    read ();
    solve ();

    return 0;
}