Cod sursa(job #2429712)

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

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

vector < unsigned int> hash1[666013];
string sir,cuv;
unsigned int ans,pow3[30],X;
unsigned int ind,l;

int exist (unsigned int  x)
{
    int k=x%mod;
    l=hash1[ind].size();
    for (int i=0;i< l ;i++)
        if (hash1[k][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))  hash1[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;

}