Cod sursa(job #1166360)

Utilizator alexalghisiAlghisi Alessandro Paolo alexalghisi Data 3 aprilie 2014 15:06:15
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <string>
#include <tr1/unordered_map>

#define LL long long
using namespace std;
using namespace tr1;


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

unordered_map < LL , bool > mp;
string x;
int n,rez = 0;

void read(){

    getline(f,x);

    for(string a;getline(f,a);){

        n = a.size();
        LL nr = 0 ;
        for(int i=0;i<n;++i)
            nr = nr * 3 + a[i] - 'a';
        mp[nr] = 1;
    }
}

void solve(){

    int len = x.size();
    LL nr = 0 , p = 1;

    for(int i=0;i<n;++i){

        nr = nr * 3 + x[i] - 'a';
        p = p * 3;
    }
    p/=3;
    rez+=mp[nr];
    for(int i=n;i<len;++i){

        nr-=p*(x[i-n]-'a');
        nr= nr*3 + x[i] - 'a';
        rez+=mp[nr];
    }
}

void write(){

    g<<rez;
}

int main()
{
    read();
    solve();
    write();
    return 0;
}