Cod sursa(job #3235121)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 15 iunie 2024 09:30:52
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <vector>

#define mod 1000000009
#define int long long
#define SIGMA 62
using namespace std;

std::vector<int> ans;
std::string s, s2;
int hash1 = 0, hash2 = 0, p = 1, p2;

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

int getCod(char ch)
{
    if(ch >= 'A' && ch <= 'Z') return ch - 'A';
    else if(ch >= 'a' && ch <= 'z') return ch - 'a' + 26;
    else return ch - '0' + 52;
}

signed main()
{
    cin >> s >> s2;
    for(int i = s.size() - 1;i >= 0; i--)
    {
        int cod = getCod(s[i]), cod2 = getCod(s2[i]);
        hash1 = (hash1 + p * cod) % mod;
        hash2 = (hash2 + p * cod2) % mod;
        p2 = p;
        p = (p * SIGMA) % mod;
    }
    p = p2;
    for(int i = s.size();i < s2.size(); i++)
    {
        int cod = getCod(s2[i]), cod2 = getCod(s2[i-s.size()]);
        if(hash1 == hash2)
            ans.push_back(i-s.size());
        hash2 = (SIGMA * ((hash2 - (p * cod2) % mod + 2 * mod) % mod) + cod) % mod;
    }
    if(hash1 == hash2)
        ans.push_back(s2.size() - s.size());
    int nr = 0;
    cout << ans.size() << '\n';
    for(int i = 0;i < ans.size() && nr < 1000; i++)
        cout << ans[i] << ' ', nr++;
}