Cod sursa(job #2441973)

Utilizator DavidLDavid Lauran DavidL Data 22 iulie 2019 12:29:39
Problema Abc2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <bits/stdc++.h>
using namespace std;

#define cin fi
#define cout fo
ifstream fi("abc2.in");
ofstream fo("abc2.out");
 
const int baza = 3;
const int MOD = 999983;

int n, m;
char A[10000005];
char a[25];
list <int> M[1000000];

void baga(long long x)
{
    int grup = x % MOD;

    bool gasit = 0;
    for (auto da: M[grup])
        if (da == x)
            gasit = 1;

    if (!gasit)
        M[grup].push_front(x);
}

bool este(long long x)
{
    int grup = x % MOD;

    for (auto da: M[grup])
        if (da == x)
            return 1;
    
    return 0;
}
 
int main()
{
    cin >> A + 1;
    n = strlen(A + 1);
 
    while (cin >> a + 1)
    {
        m = strlen(a + 1);
        long long cod = 0;
        for (int i = 1; i <= strlen(a + 1); i++)
        {
            cod = cod * baza + (a[i] - 'a');
        }
        baga(cod);
    }
 
    if (n < m)
    {
        cout << 0;
        return 0;
    }
 
    int rez = 0;
 
    long long p = 1;
    for (int i = 1; i < m; i++)
        p *= baza;
 
    long long cod = 0;
    for (int i = 1; i <= m; i++)
    {
        cod = cod * baza + (A[i] - 'a');
    }
 
    if (este(cod))
        rez++;
 
    for (int i = m + 1; i <= n; i++)
    {
        cod -= p * (A[i - m] - 'a');
        cod = cod * baza + (A[i] - 'a');
 
        if (este(cod))
            rez++;
    }
 
    cout << rez;
 
    return 0;
}