Cod sursa(job #3215714)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 15 martie 2024 12:05:35
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.85 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <cstring>

///#include <tryhardmode>
///#include <GODMODE::ON>

using namespace std;

#define cin fin
#define cout fout

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

const int BASE=73;
const int MOD1=666013;
const int MOD2=1e5+7;

vector<int>v;
string a;
string b;

int lgput(int a,int b,int MOD)
{
    if(b==0)
        return 1;
    else
    {
        if(b%2==0)
        {
            int total=lgput(a,b/2,MOD)%MOD;
            total=total*total%MOD;
            return total;
        }
        else
        {
            int total=lgput(a,b/2,MOD)%MOD;
            total=total*total%MOD;
            return total*a%MOD;
        }
    }
}

void rabin(int n,int m)
{
    int i,j;
    int hash1a=0,hash1b=0,hash2a=0,hash2b=0,p1=1,p2=1;
    for(i=0;i<n;i++)
    {
        hash1a=(hash1a*BASE+a[i])%MOD1;
        hash2a=(hash2a*BASE+a[i])%MOD2;
        hash1b=(hash1b*BASE+b[i])%MOD1;
        hash2b=(hash2b*BASE+b[i])%MOD2;
        if(i!=0)
        {
            p1=p1*BASE%MOD1;
            p2=p2*BASE%MOD2;
        }
    }
    if(hash1a==hash1b && hash2a==hash2b)
        v.push_back(0);
    //int p1=lgput(BASE,n-1,MOD1);
    //int p2=lgput(BASE,n-1,MOD2);
    for(i=n;i<=m;i++)
    {
        hash1b=(((hash1b-(b[i-n]*p1%MOD1)+MOD1)%MOD1)*BASE+b[i])%MOD1;
        hash2b=(((hash2b-(b[i-n]*p2%MOD2)+MOD2)%MOD2)*BASE+b[i])%MOD2;
        if(hash1a==hash1b && hash2a==hash2b)
            v.push_back(i-n+1);
    }
}

int main()
{
    int i,j,n,m,debuff=0;
    cin>>a>>b;
    n=a.size();
    m=b.size();
    rabin(n,m);
    cout<<v.size()<<"\n";
    for(auto i:v)
    {
        debuff++;
        cout<<i<<" ";
        if(debuff==1000)
            exit(0);
    }
    return 0;
}