Cod sursa(job #3346587)

Utilizator Gabriel_DaescuDaescu Gabriel Florin Gabriel_Daescu Data 14 martie 2026 13:44:15
Problema Matrix Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <fstream>
#define NMAX 1002
using namespace std;
ifstream  fin("matrix.in");
ofstream fout("matrix.out");
int N,M,Sp[NMAX][NMAX],ok[NMAX][NMAX];
char fb[27],A[NMAX][NMAX];

void citire()
{
    fin>>M>>N;

    for(int i=1; i<=M; i++)
    {
        for(int j=1; j<=M; j++)
        {
            fin>>A[i][j];
        }
    }

    char ch;
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=N; j++)
        {
            fin>>ch;
            fb[ch-'a']++;
        }
    }
}

int main()
{
    citire();

    for(int i=N; i<=M; i++)
    {
        for(int j=N; j<=M; j++)
        {
            ok[i][j]=1;
        }
    }

    for(int c=0; c<=25; c++)
    {
        for(int i=1; i<=M; i++)
        {
            for(int j=1; j<=M; j++)
            {
                int cost=0;
                if(A[i][j]-'a'==c)
                {
                    cost=1;
                }
                Sp[i][j]=cost+Sp[i-1][j]+Sp[i][j-1]-Sp[i-1][j-1];
            }
        }

        for(int i=N; i<=M; i++)
        {
            for(int j=N; j<=M; j++)
            {
                if(Sp[i][j]-Sp[i-N][j]-Sp[i][j-N]+Sp[i-N][j-N]!=fb[c])
                {
                    ok[i][j]=0;
                }
            }
        }
    }

    int ans=0;
    for(int i=N; i<=M; i++)
    {
        for(int j=N; j<=M; j++)
        {
            ans+=ok[i][j];
        }
    }

    fout<< ans << "\n";

    return 0;
}