Cod sursa(job #1479668)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 31 august 2015 20:32:12
Problema Matrix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <iostream>
#include <fstream>
#include <bitset>
#include <cstring>

using namespace std;

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

const int NMax = 1005;
const int Sigma = 26;

int b[Sigma], D[NMax][NMax], S[NMax][NMax];
char a[NMax][NMax];

bitset < NMax > still[NMax];

int main(){
    int n, m, ans;
    char ch;
    fin >> n >> m;
    for(int i = 1; i <= n; i++){
        fin >> (a[i] + 1);
    }
    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= m; j++){
            fin >> ch;
            b[ch - 'a']++;
        }
    }
    ans = (n - m + 1) * (n - m + 1);
    for(int k = 0; k < Sigma; k++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                D[i][j] = D[i - 1][j] + D[i][j - 1] - D[i - 1][j - 1];
                if(a[i][j] - 'a' == k){
                    D[i][j]++;
                }
            }
        }
        for(int i = m; i <= n; i++){
            for(int j = m; j <= n; j++){
                if(!still[i][j]){
                    S[i][j] = D[i][j] - D[i - m][j] - D[i][j - m] + D[i - m][j - m];
                    if(S[i][j] != b[k]){
                        still[i][j] = 1;
                        ans--;
                    }
                }
            }
        }
        memset(S, 0, sizeof(S));
    }
    fout << ans;
    return 0;
}