Cod sursa(job #2549329)

Utilizator tomaionutIDorando tomaionut Data 17 februarie 2020 16:40:11
Problema Matrix Scor 100
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;
int a[1005][1005];
int b[1005][1005];

int main()
{
    int i,j,cod1,cod2,x,y,sol=0;
    char ch;
    cod1=cod2=0;
    fin >> n >> m;
    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][j-1]+a[i-1][j]-a[i-1][j-1];
        b[i][j]=b[i][j]+b[i][j-1]+b[i-1][j]-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++)
    {
        x=a[i][j]-a[i-m][j]-a[i][j-m]+a[i-m][j-m];
        y=b[i][j]-b[i-m][j]-b[i][j-m]+b[i-m][j-m];
        if (x==cod1 and y==cod2)
            sol++;
    }
    fout << sol;


    return 0;
}