Pentru această operație este nevoie să te autentifici.

Cod sursă (job #579161)

Utilizator avatar Maftei_Andrei Maftei Andrei Iulian Maftei_Andrei IP ascuns
Problemă Fotografie (clasele 9-10) Compilator cpp-32 | 2,52 kb
Rundă Arhiva de probleme Status evaluat
Dată 21 ian. 2021 20:14:22 Scor 60
#include<fstream>
#include<iostream>
#define P 73
#define MOD1 100007
#define MOD2 100021
using namespace std;


int i, n, m, ok, N, M;
char mat1[1001][1001], mat2[1001][1001], s[1001];
int col1[1001], col2[1001];
int main()
{
    freopen("fotografie.in", "r", stdin);
    freopen("fotografie.out", "w", stdout);


    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
    {
        scanf(" %s ", &s);
        for(int j = 1; j <= m; j++)
        {
            mat1[i][j] = s[j - 1] - 'a' + 1;

        }
    }
    scanf("%d%d", &N, &M );
    for(int i = 1; i <= N; i++)
    {
        scanf(" %s ", &s);
        for(int j = 1; j <= m; j++)
        {
            mat2[i][j] = s[j - 1] - 'a' + 1;

        }
    }
    int pow1 = 1, pow2 = 1, Pow1 = 1, Pow2 = 1;
    for(int i = 1; i <= N - 1; i++)
    {
        pow1 = (pow1 * P) % MOD1;
        pow2 = (pow2 * P) % MOD2;
    }
    for(int i = 1; i <= N * (M - 1); i++)
    {
        Pow1 = (Pow1 * P) % MOD1;
        Pow2 = (Pow2 * P) % MOD2;
    }

    int has1 = 0, has2 = 0;
    for(int j = 1; j <= M; j++)
    {
        for(int i = 1; i <= N; i++)
        {
            has1 = (has1 * P + mat2[i][j]) % MOD1;
            has2 = (has2 * P + mat2[i][j]) % MOD2;
        }
    }
    int H1 = 0, H2 = 0;
    int pow12 = pow1 * P % MOD1;
    int pow22 = pow2 * P % MOD2;
    for(int i = 1; i <= n; i++)
    {
        H1 = 0, H2 = 0;
        for(int j = 1; j <= m; j++)
        {
            if(i >= N)
            {
                col1[j] = (1ll * (((col1[j] - 1ll * mat1[i - N][j] * pow1)) % MOD1 + MOD1) * P + mat1[i][j]) % MOD1;

                col2[j] = (col2[j] - 1ll * mat1[i - N][j] * pow2) % MOD2 + MOD2;
                col2[j] = (col2[j] * P + mat1[i][j]) % MOD2;
            }
            else
            {
                col1[j] = (col1[j] * P + mat1[i][j]) % MOD1;
                col2[j] = (col2[j] * P + mat1[i][j]) % MOD2;
            }
            if(j >= M)
            {
                H1 = (1ll * ((H1 - 1ll * col1[j - M] * Pow1) % MOD1 + MOD1) * pow12 + col1[j]) % MOD1;

                H2 = (1ll * ((H2 - 1ll * col2[j - M] * Pow2) % MOD2 + MOD2) * pow22 + col2[j]) % MOD2;

                if(H1 == has1 && H2 == has2)
                {
                    printf("%d %d\n", i-N, j-M);

                }
            }
            else
            {
                H1 = (1ll * H1 * pow12 + col1[j]) % MOD1;
                H2 = (1ll * H2 * pow22 + col2[j]) % MOD2;
            }
        }
    }
}