Problem p00209 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int>
using namespace std;

int a, b;
vector<P> V;
unordered_map<int, unordered_map<int, int>> c;
unordered_map<int, unordered_map<int, int>> f;
unordered_map<int, unordered_map<int, int>> g;
void kaiten() {
  int sum = 0;
  for (int i = 0; i < b; i++) {
    for (int j = b - 1; j >= 0; j--, sum++) {
      g[sum / b][sum % b] = f[j][i];
    }
  }
  for (int i = 0; i < b; i++) {
    for (int j = 0; j < b; j++) {
      f[i][j] = g[i][j];
    }
  }
}
signed main() {
  while (cin >> a >> b, a | b) {
    V.clear();
    for (int d = 0; d < a; d++) {
      for (int e = 0; e < a; e++) {
        scanf("%d", &c[d][e]);
      }
    }
    for (int d = 0; d < b; d++) {
      for (int e = 0; e < b; e++) {
        scanf("%d", &f[d][e]);
      }
    }
    for (int i = 0; i < 4; i++) {
      kaiten();
      for (int x = 0; x < a - b + 1; x++) {
        for (int y = 0; y < a - b + 1; y++) {
          bool S = true;
          for (int n = 0; n < b; n++) {
            for (int m = 0; m < b; m++) {
              if (f[n][m] != -1 && c[x + n][y + m] != f[n][m])
                S = false;
            }
          }
          if (S) {
            for (int n = 0; n < b; n++) {
              for (int m = 0; m < b; m++) {
                if (f[n][m] != -1) {
                  V.push_back(P(x + n, y + m));
                  break;
                }
              }
            }
          }
        }
      }
    }
    if (V.size() >= 1) {
      P Q = P(1 << 29, 1 << 29);
      for (P i : V) {
        Q = min(Q, i);
      }
      cout << Q.second + 1 << " " << Q.first + 1 << endl;
    } else {
      puts("NA");
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int>
using namespace std;

int a, b;
vector<P> V;
int c[100][100];
int f[50][50];
int g[50][50];
void kaiten() {
  int sum = 0;
  for (int i = 0; i < b; i++) {
    for (int j = b - 1; j >= 0; j--, sum++) {
      g[sum / b][sum % b] = f[j][i];
    }
  }
  for (int i = 0; i < b; i++) {
    for (int j = 0; j < b; j++) {
      f[i][j] = g[i][j];
    }
  }
}
signed main() {
  while (cin >> a >> b, a | b) {
    V.clear();
    for (int d = 0; d < a; d++) {
      for (int e = 0; e < a; e++) {
        scanf("%d", &c[d][e]);
      }
    }
    for (int d = 0; d < b; d++) {
      for (int e = 0; e < b; e++) {
        scanf("%d", &f[d][e]);
      }
    }
    for (int i = 0; i < 4; i++) {
      kaiten();
      for (int x = 0; x < a - b + 1; x++) {
        for (int y = 0; y < a - b + 1; y++) {
          bool S = true;
          for (int n = 0; n < b; n++) {
            for (int m = 0; m < b; m++) {
              if (f[n][m] != -1 && c[x + n][y + m] != f[n][m])
                S = false;
            }
          }
          if (S) {
            for (int n = 0; n < b; n++) {
              for (int m = 0; m < b; m++) {
                if (f[n][m] != -1) {
                  V.push_back(P(x + n, y + m));
                  break;
                }
              }
            }
          }
        }
      }
    }
    if (V.size() >= 1) {
      P Q = P(1 << 29, 1 << 29);
      for (P i : V) {
        Q = min(Q, i);
      }
      cout << Q.second + 1 << " " << Q.first + 1 << endl;
    } else {
      puts("NA");
    }
  }
}