Cod sursa(job #2004136)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 25 iulie 2017 00:01:53
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>
#include <algorithm>
#include <map>
#include <vector>
#define MOD1 30103
#define MOD2 666013
#define LL long long

using namespace std;

ifstream fin("abc2.in");
ofstream fout("abc2.out");

int N, M, i, j, ANS;
LL nr, P1;
string T, P;
map <LL, bool> Hash;

int main()
{
    fin >> T;
    N=T.size();
    while (fin >> P)
    {
        M=P.size();
        nr=0;
        P1=1;
        for (i=M-1; i>=0; i--)
        {
            nr+=P1*(P[i]-'a');
            if (i>0)
              P1*=3;
        }
        Hash[nr]=true;
    }
    nr=0;
    P1=1;
    for (i=M-1; i>=0; i--)
    {
        nr+=P1*(T[i]-'a');
        if (i>0)
          P1*=3;
    }
    if (Hash[nr]==true)
      ANS++;
    for (i=M; i<N; i++)
    {
        nr-=P1*(T[i-M]-'a');
        nr*=3;
        nr+=T[i]-'a';
        if (Hash[nr]==true)
          ANS++;
    }
    fout << ANS << '\n';
    fin.close();
    fout.close();
    return 0;
}