Cod sursa(job #3213772)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 13 martie 2024 14:00:34
Problema Potrivirea sirurilor Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
using pii = pair<int,int>;
ifstream cin("strmatch.in");
ofstream cout("strmatch.out");
const int base = 257;
const int mod = 1e9 + 7;
string a , b;
long long pw = 1;
vector <int> ans;
int cnt;
signed main()
{
    cin >> a >> b;
    for(int i = 1 ; i < a.size() ; ++i) pw = (pw * base) %mod;
    long long h = 0 , h2 = 0;
    for(int i = 0 ; i < a.size() ; ++i)
    {
        h = (h * base %mod + a[i])%mod;
        h2 = (h2 * base %mod + b[i])%mod;
    }
    cout << h << '\n';
    for(int i = 0 ;; ++i)
    {
        if(h == h2)
        {
            ++cnt;
            if(ans.size() < 1000) ans.push_back(i);
        }
        h2 -= (pw*b[i])%mod;
        while(h2 < 0) h2 += mod;
        if(i + a.size() < b.size()) h2 = (h2*base%mod + b[i + a.size()])%mod;
        else break;
    }
    cout << cnt << '\n';
    for(auto it : ans) cout << it << ' ';
    return 0;
}