Cod sursa(job #2284585)

Utilizator CyborgSquirrelJardan Andrei CyborgSquirrel Data 17 noiembrie 2018 11:44:29
Problema Potrivirea sirurilor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.84 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

typedef long long yourmom;

ifstream fin("strmatch.in");
ofstream fout("strmatch.out");

istream & in = fin;
ostream & out = fout;

char a[2000041], b[2000041];
int lolle;

int anslen = 0;
int ans[1041];

yourmom Kapow(yourmom a, int p, yourmom m)
{
    yourmom r = 1;
    for(int i = 0; i < p; i++){
        r *= a;
        r %= m;
    }
    return r;
}

struct Thing{
    yourmom n, m, p;
    Thing(yourmom n, yourmom m) : n(n), m(m){}
    void GenP()
    {
        this->p = Kapow(n, lolle-1, m);
    }
};

Thing t1(31, 60041), t2(53, 304141);

void Read()
{
    in.getline(a, 2000000);
    in.getline(b, 2000000);
    lolle = strlen(a);
    t1.GenP(); t2.GenP();
}

yourmom GenHash(const Thing & t, const char s[2000041])
{
    yourmom h = 0;
    for(auto i = s; i < s+lolle; i++){
        h *= t.n;
        h += *i;
        h %= t.m;
    }
    return h;
}

//they see me rolling
void Roll(yourmom & h, const Thing & t, const int & pos)
{
    h -= b[pos] * t.p;
    h %= t.m;
    
    h += t.m;
    h *= t.n;
    h %= t.m;
    
    h += b[pos + lolle];
    h %= t.m;
}

void Solve()
{
    yourmom c1 = GenHash(t1, a), c2 = GenHash(t2, a);
    yourmom h1 = GenHash(t1, b), h2 = GenHash(t2, b);
    int idk = strlen(b);
    for(int i = 0; i < idk-lolle-1; i++){
        if(c1 == h1 && c2 == h2){
            if(anslen < 1000){
                ans[anslen] = i;
            }
            anslen++;
        }
        Roll(h1, t1, i);
        Roll(h2, t2, i);
    }
}

void Write()
{
//    out << anslen << "\n";
//    anslen = min(anslen, 1000);
//    for(int i = 0; i < anslen; i++){
//        out << ans[i] << " ";
//    }
}

int main()
{
    Read();
    Solve();
    Write();
    return 0;
}