Cod sursa(job #3321954)

Utilizator alexiabortunBortun Alexia alexiabortun Data 11 noiembrie 2025 20:05:49
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <fstream>
#include <unordered_map>
#include <cstring>
#include <cmath>
using namespace std;
ifstream fin("abc2.in");
ofstream fout("abc2.out");
///a = 0, b = 1, c = 2
unordered_map <long long, int> M;
unordered_map <long long, int> M2;
#define N 10000001
string text, cuv;

long long transforma(string s)
{
    long long nr = 0;
    for(int i = 0; i < s.size(); i++)
        nr = nr * 3 + (s[i] - 'a');
    return nr;
}
int main()
{
    fin >> text;
    while(fin >> cuv)
    {
        long long nr = transforma(cuv);
        if(M.find(nr) == M.end())
            M[nr] = 1;
    }
    int lg = cuv.size();
    string s = text.substr(0, lg);
    long long n = transforma(s);
    if(M.find(n) != M.end())
        if(M2.find(n) == M2.end())
            M2[n] = 1;
        else M2[n]++;
    long long p = pow(3, lg - 1);
    for(int i = lg; i < text.size(); i++)
    {
        n -= p * (text[i - lg] - 'a');
        n *= 3;
        n += text[i] - 'a';
        if(M.find(n) != M.end())
            if(M2.find(n) == M2.end())
                M2[n] = 1;
            else M2[n]++;
    }
    int cnt = 0;
    for(auto per : M2)
        cnt += per.second;
    fout << cnt;
    return 0;
}