Cod sursa(job #3235589)

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

const int base = 31, mod = 1000000007;

int n, m;
long long hsh[1000001], pw[1000001];
long long hshb;

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