Cod sursa(job #2196513)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 19 aprilie 2018 16:58:01
Problema Potrivirea sirurilor Scor 72
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>
#define Mod1 666013
#define Mod2 10003
#define Mod3 56003
using namespace std;
ifstream f ("strmatch.in");
ofstream g ("strmatch.out");
vector <int> sol;
char A[2000005],B[2000005];
int n,m;
int h1,h2,x1,x2,p1,p2,h3,p3;
int main()
{
    f.getline(A,2000002);
    f.getline(B,2000002);
    n = strlen(A);
    m = strlen(B);
    h1 = h2 = h3 = 0;
    p1 = p2 = p3 = 1;
    for (int i = 0; i < n; i++)
    {
        h1 = ((1LL * h1 * 127) % Mod1 + (A[i] - 'A')) % Mod1;
        h2 = ((1LL * h2 * 131) % Mod2 + (A[i] - 'A')) % Mod2;
        x1 = ((1LL * x1 * 127) % Mod1 + (B[i] - 'A')) % Mod1;
        x2 = ((1LL * x2 * 131) % Mod2 + (B[i] - 'A')) % Mod2;
    }
    for (int i = 1; i < n; i++)
    {
        p1 = (1LL * p1 * 127) % Mod1;
        p2 = (1LL * p2 * 131) % Mod2;
        p3 = (1LL * p3 * 137) % Mod3;
    }
    if (h1 == x1 && h2 == x2)
    {
        sol.push_back(0);
    }

    for (int i = n; i < m; i++)
    {
        x1 = (1LL * (x1 - ((B[i - n] - 'A') * p1) % Mod1 + Mod1) * 127 + (B[i] - 'A')) % Mod1;
        x2 = (1LL * (x2 - ((B[i - n] - 'A') * p2) % Mod2 + Mod2) * 131 + (B[i] - 'A')) % Mod2;
        if (x1 == h1 && x2 == h2)
        {
            sol.push_back(i-n+1);
        }
    }
    g << sol.size() << '\n';

    if (1000 < sol.size())
    {
        for (int i = 0; i < 1000; i++)
            g << sol[i] << " ";
    }
    else
    {
        for (int i = 0; i < sol.size(); i++)
            g << sol[i] << " ";
    }
    return 0;
}