Cod sursa(job #2059310)

Utilizator TherevengerkingSurani Adrian Therevengerking Data 6 noiembrie 2017 21:10:39
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 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 to_int(char c)
{
    if(c >= '0' && c <= '9')return (c - '0' + 1);
    else if(c >= 'A' && c <= 'Z')return (c - 'A' + 11);
    else if(c >= 'a' && c <= 'z')return (c - 'a' + 36);
}
int main()
{
    fin >> a >> b;
    m = a.size();
    for(int i = 0 ,x; i < a.size(); ++i)
    {
        x = to_int(a[i]);
        if(i > 0)bs1 = (bs1 * 27)% mod1;
        if(i > 0)bs2 = (bs2 * 37) % mod2;
        hash1 = ((1LL * hash1 * 27) + x)%mod1;
        hash2 = ((1LL * hash2 * 37) + x)%mod2;
    }
    for(int i = 0, x; i < a.size() - 1; ++i)
    {
        x = to_int(b[i]);
        hash3 = ((1LL * hash3 * 27) + x)%mod1;
        hash4 = ((1LL * hash4 * 37) + x)%mod2;
    }
    for(int i = a.size() - 1, x; i < b.size(); ++i)
    {
        x = to_int(b[i]);
        hash3 = ((1LL * hash3 * 27) + x)%mod1;
        hash4 = ((1LL * hash4 * 37) + x)%mod2;
        if(hash3 == hash1 && hash4 == hash2)
        {
            ++r;
            v.pb(i - m + 1);
        }
        hash3 = ((hash3 - (1LL * to_int(b[i - m + 1]) * bs1)) % mod1 + mod1) % mod1;
        hash4 = ((hash4 - (1LL * to_int(b[i - m + 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;
}