Cod sursa(job #2296809)

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

using namespace std;

char text[10000005], cuv[25];

vector <long long> hh[1299827];
int lung[1299827];

unsigned int p;

void hashuire(long long x)
{
  int m;
  int i;
  m = (long long)x % 1299827;
  for(i = 0; i < lung[m]; ++ i)
    if(hh[m][i] == x)
      return ;
  hh[m].push_back(x);
  ++ lung[m];
}

bool verif(long long x)
{
  int m;
  int i;
  m = (long long)x % 1299827;
  for(i = 0; i < lung[m]; ++ i)
    if(hh[m][i] == x)
      return 1;
    if(lung[m] == 1)
        return 1;
  return 0;
}

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