Cod sursa(job #3235592)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 19 iunie 2024 01:44:28
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>
using namespace std;

const int base = 127, mod = 1000000007;

int n, m;
long long hsh[2000001], pw[2000001];
long long hsha;

int main()
{
    ifstream cin("strmatch.in");
    ofstream cout("strmatch.out");
    
    string a, b;
    cin >> a >> b;
    n = b.size();
    m = a.size();
    
    pw[0] =  1;
    for(int i = 1; i <= 2000000; i++)
        pw[i] = (pw[i-1] * base) % mod;
    
    for(int i = 0; i < m; i++)
        hsha = (hsha * base + (a[i] - '0')) % mod;
    
    for(int i = 0; i < n; i++)
        hsh[i+1] = (hsh[i] * base + (b[i] - '0')) % mod;
    
    int cnt = 0;
    
    vector<int> pos;
    for(int i = m; i <= n; i++)
    {
        long long hsh_b = (hsh[i] - ((hsh[i - m] * pw[m]) % mod) + mod) % mod;
        if(hsh_b == hsha)
        {
            cnt++;
            if(cnt <= 1000)
                pos.push_back(i - m);
        }
    }
    
    cout << cnt << '\n';
    
    for(auto x : pos)
        cout << x << " ";
    return 0;
}