Problem p03804 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll LINF = 1e18;
const ll MOD = 86400;
double EPS = 1e-8;
const double PI = acos(-1);

string strN[60];
string strM[60];
int N, M;

int memo[60][60];

bool check(int x, int y) {

  bool match = true;

  for (int i = x; i < x + M; i++) {
    for (int j = y; j < y + M; j++) {
      if (strN[i][j] != strM[i - x][j - y]) {
        match = false;
      }
    }
  }

  return match;
}

bool dfs(int x, int y) {
  if (memo[x][y] != -1)
    return (bool)memo[x][y];

  if (x + M - 1 >= N or y + M - 1 >= N)
    return memo[x][y] = false;

  if (check(x, y))
    return memo[x][y] = true;

  if (dfs(x + 1, y))
    return memo[x][y] = true;
  if (dfs(x, y + 1))
    return memo[x][y] = true;

  return false;
}

int main() {

  cin >> N >> M;

  for (int i = 0; i < N; i++) {
    cin >> strN[i];
  }

  for (int j = 0; j < M; j++) {
    cin >> strM[j];
  }
  rep(i, 60) rep(j, 60) memo[i][j] = -1;
  bool rlt = dfs(0, 0);

  if (rlt) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll LINF = 1e18;
const ll MOD = 86400;
double EPS = 1e-8;
const double PI = acos(-1);

string strN[60];
string strM[60];
int N, M;

int memo[60][60];

bool check(int x, int y) {

  bool match = true;

  for (int i = x; i < x + M; i++) {
    for (int j = y; j < y + M; j++) {
      if (strN[i][j] != strM[i - x][j - y]) {
        match = false;
      }
    }
  }

  return match;
}

bool dfs(int x, int y) {
  if (memo[x][y] != -1)
    return (bool)memo[x][y];

  if (x + M - 1 >= N or y + M - 1 >= N)
    return memo[x][y] = false;

  if (check(x, y))
    return memo[x][y] = true;

  if (dfs(x + 1, y))
    return memo[x][y] = true;
  if (dfs(x, y + 1))
    return memo[x][y] = true;

  return memo[x][y] = false;
}

int main() {

  cin >> N >> M;

  for (int i = 0; i < N; i++) {
    cin >> strN[i];
  }

  for (int j = 0; j < M; j++) {
    cin >> strM[j];
  }
  rep(i, 60) rep(j, 60) memo[i][j] = -1;
  bool rlt = dfs(0, 0);

  if (rlt) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}