Cod sursa(job #2924290)

Utilizator XXMihaiXX969Gherghinescu Mihai Andrei XXMihaiXX969 Data 28 septembrie 2022 20:54:59
Problema Potrivirea sirurilor Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.9 kb
#include <bits/stdc++.h>

using namespace std;

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

#define int long long

const int mod1 = 7919;
const int mod2 = 7879;

const int exponential = 29791;

main()
{
    string s1, s2;
    getline(in, s1);
    getline(in, s2);
    vector <int> rez;
    
    int hashedValueMod1 = 0;
    int hashedValueMod2 = 0;
    int matchedHashedMod1 = 0;
    int matchedHashedMod2 = 0;
    int p1 = 1;
    int p2 = 1;
    for (int i = 0; i < s1.size(); i++)
    {
        hashedValueMod1 = ((hashedValueMod1 * exponential) % mod1 
            + s1[i]) % mod1; 
        hashedValueMod2 = ((hashedValueMod2 * exponential) % mod2 
            + s1[i]) % mod2;
        matchedHashedMod1 = ((matchedHashedMod1 * exponential) % mod1 
            + s2[i]) % mod1; 
        matchedHashedMod2 = ((matchedHashedMod2 * exponential) % mod2 
            + s2[i]) % mod2;
        if (i != 0)
        {
            p1 = (p1 * exponential) % mod1;
            p2 = (p2 * exponential) % mod2;
        }
    }
    if (hashedValueMod1 == matchedHashedMod1 &&
        hashedValueMod2 == matchedHashedMod2)
        rez.push_back(0);

    for (int i = s1.size(); i < s2.size(); i++)
    {
        
        matchedHashedMod1 = ((matchedHashedMod1 - 
            (p1 * s2[i - s1.size()]) % mod1 + mod1)
                * exponential
                    + s2[i]) % mod1;
        matchedHashedMod2 = ((matchedHashedMod2 - 
            (p2 * s2[i - s1.size()]) % mod2 + mod2) 
                * exponential 
                    + s2[i]) % mod2;
        if (hashedValueMod1 == matchedHashedMod1 &&
        hashedValueMod2 == matchedHashedMod2)
            rez.push_back(i - s1.size() + 1);
    }
    out << rez.size() <<'\n';
    
    int toDisplay = min((int)rez.size(), 1000ll);
    for (int i = 0; i < toDisplay; i++)
    {
        out << rez[i] << " ";
    }
}