Cod sursa(job #2323950)

Utilizator Moise_AndreiMoise Andrei Moise_Andrei Data 20 ianuarie 2019 08:53:47
Problema Matrix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("matrix.in");
ofstream out("matrix.out");
char v[1005][1005];
int c[1005][1005];
int s[1005][1005];
int fr[30];
int m, n;

int f()
{
    int nrapp = 0, nr = 0;
    for(int cifra = 0; cifra < 27; cifra ++)
    {
        if(fr[cifra])
        {
            nrapp ++;
            for(int i = 1; i <= n; i ++)
            {
                for(int j = 1; j <= n; j ++)
                {
                    s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
                    if(v[i][j] - 'a' == cifra)
                        s[i][j] ++;
                }
            }
            for(int i = m; i <= n; i ++)
            {
                for(int j = m; j <= n; j ++)
                {
                    int x = i - m;
                    int y = j - m;
                    if(fr[cifra] == (s[i][j] - s[x][j] - s[i][y] + s[x][y]))
                        c[i][j] ++;
                }
            }
        }
    }
    for(int i = m; i <= n; i ++)
    {
        for(int j = m; j <= n; j ++)
        {
            if(c[i][j] == nrapp)
                nr ++;
        }
    }
    return nr;
}

void citire()
{
    in >> n >> m;
    for(int i = 1; i <= n; i ++)
        in >> v[i] + 1;
    for(int i = 1; i <= m * m; i ++)
    {
        char ch;
        in >> ch;
        fr[ch - 'a'] ++;
    }
}

int main()
{
    citire();
    out << f();
    return 0;
}