Cod sursa(job #2670106)

Utilizator Rares5000Baciu Rares Rares5000 Data 9 noiembrie 2020 01:08:12
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 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 int> h[P + 10];
string txt, cuv;

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

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