Cod sursa(job #2844988)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 6 februarie 2022 20:07:44
Problema Matrix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("matrix.in");
ofstream fout("matrix.out");

int n, m, sV, s2V, sH[1005][1005], s2H[1005][1005], sol;

int main()
{
    char ch;
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            fin >> ch;
            sH[i][j] = sH[i-1][j] + sH[i][j-1] - sH[i-1][j-1] + (ch - 'a' + 1);
            s2H[i][j] = s2H[i-1][j] + s2H[i][j-1] - s2H[i-1][j-1] + (ch - 'a' + 1)*(ch - 'a' + 1);
        }
    for(int i = 1; i <= m; i++)
        for(int j = 1; j <= m; j++)
    {
        fin >> ch;
        sV += (ch - 'a' + 1);
        s2V += ((ch - 'a' + 1)*(ch - 'a' + 1));
    }
    for(int i = m; i <= n; i++)
        for(int j = m; j <= n; j++)
    {
        int s1 = sH[i][j] - sH[i-m][j] - sH[i][j-m] + sH[i-m][j-m];
        int s2 = s2H[i][j] - s2H[i-m][j] - s2H[i][j-m] + s2H[i-m][j-m];
        if(s1 == sV && s2 == s2V)
            sol++;
    }
    fout << sol << "\n";
    fout.close();
    return 0;
}