Cod sursa(job #2228049)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 2 august 2018 16:11:45
Problema Matrix Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <iostream>
using namespace std;

int n, m;
int up[1010][26] = {}, down[1010][26] = {};
char mat[1010][1010];

void apply(int stat[1010][26], char line[1010]){
    int curr[26] = {};
    for(int i = 0; i < m; ++i){
        curr[line[i] - 'a'] += 1;
        for(int j = 0; j < 26; ++j)
            stat[i+1][j] += curr[j]; } }

int main(){
    ifstream f("matrix.in");
    ofstream g("matrix.out");
    f >> m >> n;
    for(int i = 0; i < m; ++i)
        f >> ws >> mat[i];

    for(int i = 0; i < n; ++i)
        apply(down, mat[i]);

    int target[26];
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            char ch;
            f >> ws >> ch;
            target[ch - 'a'] += 1;  } }

    int ret = 0;
    for(int i = n; i <= m; ++i){
        for(int j = n; j <= m; ++j){
            bool good = true;
            for(int k = 0; k < 26; ++k)
                good = (good && (target[k] == down[j][k] + up[j-n][k] - up[j][k] - down[j-n][k]));
            ret += (int)good; }
        if(i < m){
            apply(up, mat[i-n]);
            apply(down, mat[i]); } }

    g << ret << '\n' << ' ';
    return 0; }