Cod sursa(job #3282122)

Utilizator BOSSSTEFANPetrescu Ioan Stefan BOSSSTEFAN Data 4 martie 2025 16:06:31
Problema Matrix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;
const int nmax = 1e3;
char mat[nmax + 1][nmax + 1];
int mc[nmax + 1][nmax + 1], vc[26];
short int sol[nmax + 1][nmax + 1];
int main()
{
    ios :: sync_with_stdio(false);
    cin.tie(0);
    freopen("matrix.in", "r", stdin);
    freopen("matrix.out", "w", stdout);
    int n, m, i, j, rez = 0;
    char ch;
    cin >> n >> m;
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
            cin >> mat[i][j];
    for(i = 1; i <= m; i++)
        for(j = 1; j <= m; j++)
        {
            cin >> ch;
            vc[ch - 'a']++;
        }
    for(ch = 'a'; ch <= 'z'; ch++)
    {
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= n; j++)
            {
                mc[i][j] = mc[i - 1][j] + mc[i][j - 1] - mc[i - 1][j - 1];
                if(mat[i][j] == ch)
                    mc[i][j]++;
            }
        }
        for(i = m; i <= n; i++)
            for(j = m; j <= n; j++)
                if(mc[i][j] - mc[i - m][j] - mc[i][j - m] + mc[i - m][j - m] == vc[ch - 'a'])
                    sol[i][j]++;
    }
    for(i = m; i <= n; i++)
        for(j = m; j <= n; j++)
            if(sol[i][j] == 26)
                rez++;
    cout << rez;
    return 0;
}