Cod sursa(job #2706924)

Utilizator tomaionutIDorando tomaionut Data 16 februarie 2021 09:17:22
Problema Matrix Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("matrix.in");
ofstream fout("matrix.out");
int n,m,a[1005][1005],b[1005][1005];
int main()
{
    int i,j,cod1,cod2,ch,x,sol=0,codx,cody;
    fin >> n >> m;
    cod1=cod2=0;
    for (i=1; i<=n; i++)
    for (j=1; j<=n; j++)
    {
        fin >> ch;
        x=ch-'a'+1;
        a[i][j]=x;
        b[i][j]=x*x;
        a[i][j]=a[i][j]+a[i-1][j]+a[i][j-1]-a[i-1][j-1];
        b[i][j]=b[i][j]+b[i-1][j]+b[i][j-1]-b[i-1][j-1];
    }
    for (i=1; i<=m; i++)
        for (j=1; j<=m; j++)
    {
        fin >> ch;
        x=ch-'a'+1;
        cod1+=x;
        cod2+=x*x;
    }
    for (i=m; i<=n; i++)
        for (j=m; j<=n; j++)
    {
        codx=a[i][j]-a[i-m][j]-a[i][j-m]+a[i-m][j-m];
        cody=b[i][j]-b[i-m][j]-b[i][j-m]+b[i-m][j-m];
        if (cod1==codx and cod2==cody)
            sol++;
    }
    fout << sol << "\n";


    return 0;
}