Cod sursa(job #1587964)

Utilizator DrumeaVDrumea Vasile DrumeaV Data 2 februarie 2016 18:17:35
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

typedef long long ll;

const int Mn = 1e7 + 8;
const int mod = 666013;

int n,m,sol;
ll val,pw = 1;
char s[Mn],w[21];
vector< ll > h[mod];

bool found(ll x)
{
    int y = x % mod;

    for (int i = 0;i < h[y].size();i++)
        if (h[y][i] == x)
           return 1;

    return 0;
}

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

     scanf("%s",s);
     n = strlen(s);

     for (;!feof(stdin);)
     {
         scanf("%s",w);
         m = strlen(w);
         val = 0;

         for (int i = 0;i < m;i++)
             val = val * 3 + w[i] - 'a';

         if (!found(val))
            h[val % mod].push_back(val);
     }

     val = 0;
     for (int i = 0;i < m;i++)
         val = val * 3 + s[i] - 'a',pw *= 3;

     pw /= 3;
     sol = found(val);
     for (int i = m;i < n;i++)
     {
         val = (val -  pw * (s[i - m] - 'a')) * 3 + s[i] - 'a';
         sol += found(val);
     }

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

  return 0;
}