Cod sursa(job #2059299)

Utilizator TherevengerkingSurani Adrian Therevengerking Data 6 noiembrie 2017 20:58:28
Problema Potrivirea sirurilor Scor 14
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
#define mod1 666013
#define pb push_back
#define mod2 1000007
#define ll long long
string a, b;
ll hash1, hash2, hash3, hash4, m, r, bs1 = 1, bs2 = 1;
vector<int>v;
int main()
{
    fin >> a >> b;
    m = a.size();
    for(int i = 1; i < a.size();++i)
        bs1 *= 27, bs2 *= 37;
    for(auto i : a)
    {
        hash1 = ((1LL * hash1 * 27) % mod1 + (i - 'A' + 1))%mod1;
        hash2 = ((1LL * hash2 * 37) % mod2 + (i - 'A' + 1))%mod2;
    }
    for(int i = 0, x; i < a.size() - 1; ++i)
    {
        x = b[i] - 'A' + 1;
        hash3 = ((1LL * hash3 * 27) % mod1 + x)%mod1;
        hash4 = ((1LL * hash4 * 37) % mod2 + x)%mod2;
    }
    for(int i = a.size() - 1, x; i < b.size(); ++i)
    {
        x = b[i] - 'A' + 1;
        hash3 = ((1LL * hash3 * 27) % mod1 + x)%mod1;
        hash4 = ((1LL * hash4 * 37) % mod2 + x)%mod2;
        if(hash3 == hash1 && hash4 == hash2)
        {
            ++r;
            v.pb(i - m + 1);
        }
        hash3 = ((hash3 - (1LL * (b[i - m + 1] - 'A' + 1) * bs1)) % mod1 + mod1) % mod1;
        hash4 = ((hash4 - (1LL * (b[i - m + 1] - 'A' + 1) * bs2)) % mod2 + mod2) % mod2;
    }
    fout << v.size() << '\n';
    if(v.size() > 1000)r = 1000;
    for(int i = 0; i < r; ++i)
        fout << v[i] << " ";
    return 0;
}