Cod sursa(job #2670102)

Utilizator Rares5000Baciu Rares Rares5000 Data 9 noiembrie 2020 01:02:53
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <fstream>
#include <vector>
#include <string>
#define desync ios::sync_with_stdio(0); fin.tie(0); fout.tie(0);
#define files fin.close(); fout.close();
#define P 15029

using namespace std;

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

unsigned puteri[22], x;
vector<unsigned> h[P + 10];
string txt, cuv;

bool Search(int x){
    unsigned int i;
    int elem, len;
    elem = x % P;
    len = h[elem].size();
    for(int i = 0; i < len; i++)
        if(h[elem][i] == x) return 1;
    return 0;
}

void Solve(){
    fin >> txt;
    puteri[0] = 1;
    for(int i = 1; i <= 19; i++)
        puteri[i] = puteri[i - 1] * 3;
    int lencuv, lentxt;
    lentxt = txt.size();
    while(fin >> cuv){
        lencuv = cuv.size();
        x = 0;
        int elem;
        for(int i = 0; i < lencuv; i++)
            x += puteri[i] * (cuv[i] - 'a');
        elem = x % P;
        if(Search(x) == 0)
            h[elem].push_back(x);
    }
    x = 0;
    for(int i = 0; i < lencuv; i++)
        x += puteri[i] * (txt[i] - 'a');
    int ans = 0;
    ans += Search(x);
    for(int i = lencuv; i < lentxt; i++){
        x /= 3;
        x += puteri[lencuv - 1] * (txt[i] - 'a');
        ans += Search(x);
    }
    fout << ans;
}

int main()
{
    desync;
    Solve();
    files;
    return 0;
}