Cod sursa(job #2475000)

Utilizator alex_bb8Banilean Alexandru-Ioan alex_bb8 Data 16 octombrie 2019 00:19:04
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <bits/stdc++.h>

using namespace std;

ofstream g("abc2.out");


const int mod=18029;
vector<unsigned int> M[mod];
bool okmod[mod];
char s[25];
char x;
unsigned int p3[35],sol=0;
unsigned int n,poz,i,j;

int main()
{

    p3[0]=1;
    for(i=1;i<=20;i++)
     p3[i]=p3[i-1]*3;

    ifstream f;
    f.open("abc2.in",ifstream::in);

    //int lgp=f.tellg();
    f.ignore(10000000,'\n');

    //lgp=f.tellg();

    long long nr=0;

    while(f.getline(s,21))
    {

     for(i=0;s[i];i++)
      nr+=(s[i]-'a')*p3[i];

    unsigned int poz=nr%mod;
    bool ok=1;
    for(unsigned int it : M[poz])
     if(it==nr) {ok=0;break;}
    if(ok==1) M[poz].push_back(nr);
     nr=0;
     n=strlen(s);

    }


    f.close();

    f.open("abc2.in",ifstream::in);

    for(j=0;j<=n-1;j++)
     {f.get(x);nr+=(x-'a')*p3[j];}

    for(j=n;x!='\n';j++)
    {
     f.get(x);
     poz=nr%mod;
     if(!okmod[poz]) sort(M[poz].begin(),M[poz].end()),okmod[poz]=1;
     if(binary_search(M[poz].begin(),M[poz].end(),nr))
      sol++;
     nr=nr/3;
     nr+=(x-'a')*p3[n-1];
    }

     poz=nr%mod;
     if(!okmod[poz]) sort(M[poz].begin(),M[poz].end()),okmod[poz]=1;
     if(binary_search(M[poz].begin(),M[poz].end(),nr))
      sol++;


    g<<sol;

    f.close();

    return 0;
}