Cod sursa(job #2296770)

Utilizator cahemanCasian Patrascanu caheman Data 5 decembrie 2018 00:18:16
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>

using namespace std;

char text[10000005], cuv[25];

vector <unsigned int> hh[66013];

unsigned int p;

void hashuire(unsigned int x)
{
  unsigned int m;
  m = x % 66013;
  for(vector <unsigned int> :: iterator it = hh[m].begin(); it != hh[m].end(); ++ it)
    if((*it) == x)
      return ;
  hh[m].push_back(x);
}

bool verif(unsigned int x)
{
  unsigned int m;
  m = x % 66013;
  for(vector <unsigned int> :: iterator it = hh[m].begin(); it != hh[m].end(); ++ it)
    if((*it) == x)
      return 1;
  return 0;
}

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