Cod sursa(job #1257917)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 8 noiembrie 2014 12:18:27
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 1e7 + 1;
const int Pmax = 20 + 1;

unordered_set <long long> Set;

char s[Nmax], sir[Pmax];
int T, P;

int main()
{
    ifstream in("abc2.in");
    ofstream out("abc2.out");

    ios_base::sync_with_stdio( false );

    in >> ( s + 1 );
    T = strlen( s + 1 );

    long long base = 1;

    while ( in >> ( sir + 1 ) )
    {
        if ( !P )
            P = strlen( sir + 1 );

        long long cod = 0;

        for ( int i = 1; i <= P; ++i )
            cod = cod * 3LL + ( sir[i] - 'a' );

        Set.insert( cod );
    }

    for ( int i = 1; i < P; ++i )
        base *= 3LL;

    int sol = 0;
    long long cod = 0;

    for ( int i = 1; i <= T; ++i )
    {
        if ( i > P )
            cod -= 1LL * base * ( s[i - P] - 'a' );

        cod = cod * 3LL + ( s[i] - 'a' );

        if ( i >= P )
            sol += ( Set.find( cod ) != Set.end() );
    }

    out << sol << "\n";

    return 0;
}