Cod sursa(job #1816554)

Utilizator silkMarin Dragos silk Data 26 noiembrie 2016 16:58:09
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <cstdio>
#include <cstring>
#include <vector>
#define NMax 10000005
#define TMax 25
#define uint unsigned int
using namespace std;

const int P = 474707;

char s[NMax+1];
char w[TMax+1];
vector<uint> h[P];
uint ans;

inline void add(int x)
{
    h[x%P].push_back(x);
}

bool is(int x)
{
    int r = x%P;
    for(int i = 0; i < h[r].size(); ++i)
    if( h[r][i] == x ) return 1;
return 0;
}

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

    int i,N,T;
    uint x,pwr;

    fgets(s,NMax,stdin);
    N = strlen(s)-1;
    for(i = 0; i < N; ++i) s[i] = s[i] - 'a';

    while( fgets(w,TMax,stdin) && !feof(stdin) )
    {
        x = 0;
        T = strlen(w);
        if( w[T-1]=='\n' ) --T;

        for(i = 0; i < T; ++i) x = x * 3 + w[i] - 'a';
        add(x);
    }

    for(x = i = 0; i < T; ++i) x = x * 3 + s[i];
    if( is(x) ) ++ans;

    for(pwr = i = 1; i < T; ++i) pwr = pwr * 3 ;
    for(i = T; i < N; ++i)
    {
        x = ( x - pwr * s[i-T] ) * 3 + s[i];
        if( is(x) ) ++ans;
    }

    printf("%d\n",ans);



return 0;
}