Cod sursa(job #2297073)

Utilizator mircearoataMircea Roata Palade mircearoata Data 5 decembrie 2018 11:55:35
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda prega_casi_5.12.2018 Marime 1.45 kb
#include <fstream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

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

const int mod = 100000007;

int cnt, n, len, ans;
string ch;
string cuv;
unordered_map<long long, vector<string>> Hash;
char temp[10000001];
char *tmp2;
long long h, put3 = 1;

int main()
{
    ios_base::sync_with_stdio(false);
    getline(in, ch);
    while(!in.eof())
    {
        getline(in, cuv);
        n = max(n, (int)cuv.length());
        h = 0;
        for(int i = 0; i < n; i++)
            h = (h * 3 + cuv[i] - 'a') % mod;
        Hash[h].push_back(cuv);
    }
    len = ch.length();
    h = 0;
    for(int i = 0; i < n; i++)
    {
        temp[i] = ch[i];
        h = (h * 3 + ch[i] - 'a') % mod;
        put3 *= 3;
    }
    tmp2 = temp;
    put3 /= 3;
    for(int i = n; i < len; i++)
    {
        string str(tmp2);
        vector<string> tmpVec = Hash[h];
        for(int j = 0; j < tmpVec.size(); j++)
            if(tmpVec[j] == str)
            {
                ans++;
                break;
            }
        tmp2++;
        temp[i] = ch[i];
        h = h % put3;
        h = (h * 3 + ch[i] - 'a') % mod;
    }
    string str(tmp2);
    vector<string> tmpVec = Hash[h];
    for(int j = 0; j < tmpVec.size(); j++)
        if(tmpVec[j] == str)
        {
            ans++;
            break;
        }
    out << ans;
    return 0;
}