Cod sursa(job #2692330)

Utilizator PredescuSebastianIonPredescu Sebastian Ion PredescuSebastianIon Data 2 ianuarie 2021 12:37:08
Problema Matrix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
#include <cmath>
#include <deque>

using namespace std;
ifstream f("matrix.in");
ofstream g("matrix.out");
int n,m,ct[30],nr=0,sum[1005][1005];
char a[1005][1005],c;
bool contor[10005][1005];
int main()
{
    f>>n>>m;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            f>>a[i][j];
    for(int i=1; i<=m; i++)
        for(int j=1; j<=m; j++)
        {
            f>>c;
            ct[c-'a'+1]++;
        }
    for(int i=m; i<=n; i++)
        for(int j=m; j<=n; j++)
            contor[i][j]=1;
    for(int z=1; z<=26; z++)
    {
        sum[1][1]=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(a[i][j]-'a'+1==z)
                    sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+1;
                else
                    sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
                if(i>=m&&j>=m)
                    if(sum[i][j]-sum[i-m][j]-sum[i][j-m]+sum[i-m][j-m]!=ct[z])
                        contor[i][j]=0;
            }
        }
    }
    for(int i=m; i<=n; i++)
        for(int j=m; j<=n; j++)
            nr+=contor[i][j];
    g<<nr;
    return 0;
}