Cod sursa(job #2429679)

Utilizator Briana_NeaguNeagu Briana Briana_Neagu Data 10 iunie 2019 20:49:48
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>
#define mod 666013
using namespace std;

ifstream f ("abc2.in");
ofstream g("abc2.in")

vector <int> hash[666013];
int index, X;
string sir,cuv;
int ans,pow3[30],l;
long long ind;

int exist (long long  x)
{
    int ind=x%mod;
    int l=hash[ind].size();
    for (int i=0;i< l ;i++)
         if (hash[ind][i]==x )
               return 1;
    return 0;
}


int main()
{
  f>>sir;
  while ( f >> cuv)
  {
      X=cuv.size();
      ind=0;
      for (int i=0 ; i < X ; i++)
          ind=ind*3 + (cuv[i]-'a');
      if (!exist(ind))  hash[ind%mod].push_back(ind);
  }
  pow3[0]=1;
  for (int i=1; i<= 30 ;i++)
      pow3[i]=pow3[i-1]*3;
   l=sir.size();
   ind=0;
  for (int i= 0 ; i < X ; i ++ )
      ind=ind*3+(sir[i]-'a');
   ans+=exist(ind);
   for (int i=1; i <= l-X ; i++ )
   {
       ind-=(sir[i-1]-'a')*pow3[X-1];
       ind*=3;
       ind+=(sir[i+X-1]-'a');
       ans+=exist(ind);

   }
   g<<ans;

}