Cod sursa(job #2297669)

Utilizator cahemanCasian Patrascanu caheman Data 6 decembrie 2018 10:35:55
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include<cstdio>
#include<vector>
#include<cstring>
#include<fstream>
#pragma GCC optimize("03")
using namespace std;

char text[10000005];
char cuv[25];

vector <unsigned int> hh[10041];

void hashuire(unsigned int x)
{
  int m;
  m = x % 10041;
  for(auto it:hh[m])
    if(it== x)
      return ;
  hh[m].push_back(x);
}

bool verif(unsigned int x)
{
  int m;
  m = x % 10041;
  for(auto it:hh[m])
    if(it == x)
      return 1;
  return 0;
}

int main()
{
  freopen("abc2.in", "r", stdin);
  freopen("abc2.out", "w", stdout);
  unsigned int x, p;
  int n, i, j, nr = 0, m = 0;
  scanf("%s", &text);
  n = 0;
  n=strlen(text);
  while(scanf("%s", &cuv)!=EOF)
  {
    m = strlen(cuv);
    x = 0;
    for(i = 0; i < m; ++ i)
      x = x * 3 + cuv[i] - 'a';
    hashuire(x);
  }
  x = 0;
  p = 1;
  for(i = 1; i < m; ++ i)
    p = p * 3;
  for(i = 0; i < n; ++ i)
  {
    x = x * 3 + text[i] - 'a';
    if(i >= m - 1)
    {
        nr += verif(x);
        x = x % p;
    }
  }
  printf("%d\n", nr);
  return 0;
}