Cod sursa(job #2765303)

Utilizator DavidAA007Apostol David DavidAA007 Data 26 iulie 2021 11:44:08
Problema Potrivirea sirurilor Scor 38
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.11 kb
#include<iostream>
#include<fstream>
#include<string.h>
#define mod 100007
#define mod1 100021
#define p 140
#define p1 100
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
char sir1[2000005],sir2[2000005];
long long int n,m,i,j,c,nr,nr1,nr2,prima,ultima,ind,putere;
long long int nr11,nr21,putere1;
int main()
{
    fin.getline(sir1,2000005);
    //cin.get();
    fin.getline(sir2,2000005);
    n=strlen(sir1);
    m=strlen(sir2);
    nr1=0;
    for(i=0;i<n;i++)
    {
        nr=sir1[i];
        nr1=(nr1*p+nr)%mod;
        nr11=(nr11*p1+nr)%mod1;
    }
    putere=1;
    putere1=1;
    for(i=0;i<n;i++)
    {
        if(i>0)
        {
            putere=(putere*p)%mod;
            putere1=(putere1*p1)%mod1;
        }
        nr=sir2[i];
        nr2=(nr2*p+nr)%mod;
        nr21=(nr21*p1+nr)%mod1;
    }
    //cout<<nr21<<"**\n";
    if(nr2==nr1 && nr21==nr11)
        c++;
    ultima=nr;
    for(i=n;i<m;i++)
    {
        nr=sir2[i];
        prima=sir2[i-n];
        nr2=(nr2-prima*putere)%mod;
        nr21=(nr21-prima*putere1)%mod1;
        nr2=(nr2+mod)%mod;
        nr21=(nr21+mod1)%mod1;
        nr2=(nr2*p+nr)%mod;
        nr21=(nr21*p1+nr)%mod1;
        //cout<<nr21<<" "<<nr11<<"\n";
        if(nr2==nr1 && nr21==nr11)
        {
            c++;
        }
    }
    fout<<c<<"\n";
    nr=0;
    nr2=0;
    nr21=0;
    putere=1;
    putere1=1;
    for(i=0;i<n;i++)
    {
        if(i>0)
        {
            putere=(putere*p)%mod;
            putere1=(putere1*p1)%mod1;
        }
        nr=sir2[i];
        nr2=(nr2*p+nr)%mod;
        nr21=(nr21*p1+nr)%mod1;
    }
    //cout<<nr21<<"**\n";
    if(nr2==nr1 && nr21==nr11)
        c++;
    ultima=nr;
    for(i=n;i<m;i++)
    {
        nr=sir2[i];
        prima=sir2[i-n];
        nr2=(nr2-prima*putere)%mod;
        nr21=(nr21-prima*putere1)%mod1;
        nr2=(nr2+mod)%mod;
        nr21=(nr21+mod1)%mod1;
        nr2=(nr2*p+nr)%mod;
        nr21=(nr21*p1+nr)%mod1;
        //cout<<nr21<<" "<<nr11<<"\n";
        if(nr2==nr1 && nr21==nr11)
        {
            fout<<i-n+1<<" ";
            c++;
        }
    }
    fin.close();
    fout.close();
    return 0;
}