Cod sursa(job #1725713)

Utilizator andreistefyAndrei andreistefy Data 6 iulie 2016 11:44:16
Problema Potrivirea sirurilor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
#include <vector>
#define tip long long
using namespace std;

ifstream f("strmatch.in");
ofstream g("strmatch.out");
string a,b;
tip A, B, i, j, cnt, ap[1010], ha, hb, x=101LL, m=1000000007LL, u, v, putere(tip, tip);
int main()
{
    f>>a>>b;
    A=a.size();
    B=b.size();
    for(i=0, j=1; i<A; i++,j=(j*x)%m)
    {
        ha=(ha+a[i]*j)%m;
        hb=(hb+b[i]*j)%m;
    }
    if(ha==hb)
    {
        cnt++;
        if(cnt<=1000)
            ap[cnt]=0;
    }
    u=putere(x, m-2);
    v=putere(x, A-1);
    for(j=0,i=A; i<B; i++,j++)
    {
        hb=((hb+m-b[j])*u+v*b[i])%m;
        if(ha==hb)
        {
            cnt++;
            if(cnt<=1000)
                ap[cnt]=j+1;
        }
    }
    g<<cnt<<"\n";
    j=min(1000LL, cnt);
    for(i=1; i<=j; i++)
        g<<ap[i]<<' ';
    return 0;
}

tip putere(tip b, tip e)
{
    tip r = 1;
    for(;e;e/=2)
    {
        if(e%2==1)
        r=(r*b)%m;
        b=(b*b)%m;
    }
    return r;
}