Cod sursa(job #2587566)

Utilizator marius004scarlat marius marius004 Data 22 martie 2020 23:48:53
Problema Potrivirea sirurilor Scor 14
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

ifstream f("strmatch.in");
ofstream g("strmatch.out");

const long long MOD = 10000007;
const long long Prime = 31;

long long hashA,hashB,putere;

const int NMAX =  2000005;
char a[NMAX],b[NMAX];

int n,m;
vector<int>sol;

/// a e patternul

int order(char c){
    return (c >= 'a' && c <= 'z') ? c - 'a' : c - 'A';
}

long long lgput(long long a,long long b){

    long long r = 1;

    while(b){

        if(b & 1)
            r = r * a;

        a *= a;
        b >>= 1;
    }

    return r;
}

int main(){

    f >> a >> b;

    n = strlen(a);
    m = strlen(b);

    if(n > m){
        g << 0;
        return 0;
    }

    putere = 1;
    for(int i = 0;i < n;++i){
        hashA = (hashA + putere * order(a[i])) % MOD;
        hashB = (hashB + putere * order(b[i])) % MOD;
        putere =(putere * Prime) % MOD;
    }

    putere /= Prime;

    if(hashA == hashB)
        sol.push_back(0);

    for(int i = n;i < m;++i){

        hashB -= order(b[i - n]);

        if(hashB < 0)
            hashB += MOD;

        hashB /= Prime;
        hashB = (hashB + putere * order(b[i])) % MOD;

        if(hashA == hashB)
            sol.push_back(i - n + 1);
    }

    for(int i = 0;i < sol.size();++i)
        g << sol[i] << ' ';

    return 0;
}