Problem p00469 (C++) — buggy submission, verdict: Memory Limit Exceeded

// include----------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
// using
// namespace--------------------------------------------------------------------------------------------------------------
using namespace std;

// define-----------------------------------------------------------------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, s, n) for (int i = s; i < (int)n; i++)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n)                                                             \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\\n";
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << "\\n";
#define sq(n) (n) * (n)

// typedef----------------------------------------------------------------------------------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
// const------------------------------------------------------------------------------------------------------------------------
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
// global-----------------------------------------------------------------------------------------------------------------------
int n, m, ans;
VS ar;
set<string> s;
// #define DEBUG
// int dp[200000000][]
int solve(int now, string str) {
  if (now == m) {
    if (s.count(str)) {
      return 0;
    }
    s.insert(str);
    return 1;
  }
  int res = 0;
  for (int i = now; i < n; i++) {
    swap(ar[now], ar[i]);
    res += solve(now + 1, str + ar[now]);
    swap(ar[now], ar[i]);
  }
  return res;
}
int main() {
  while (scanf("%d %d", &n, &m) != EOF && n && m) {
    ar.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> ar[i];
    }
    SORT(ar);
    printf("%d\\n", solve(0, ""));
    s.clear();
  }
  return 0;
}

Error: MLE

Fixed submission:

/* AOJ 0546
 *
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0546
 *
 */

// include
#include <bits/stdc++.h>

// using
using namespace std;

// define static const typedef
#define FOR(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define REP(i, n) FOR((i), 0, (n))
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define DUMP(x) cerr << #x << " = " << x << "\\n";
#define DEBUG(x)                                                               \\
  cerr << #x << " = " << x << " (L:" << __LINE__ << ") " << __FILE__ << "\\n";
#define F first
#define S second

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

static const int INF = 1 << 25;

// global
static set<pair<string, VS>> e;
static int n, k, ans;
static VS str_array;
static VS ans_string;
static char b[2];
// function
static void solve(int many_used, string now_string, VS can_use) {
  if (e.count(pair<string, VS>(now_string, can_use))) {
    return;
  }
  e.insert(pair<string, VS>(now_string, can_use));
  if (k == many_used && !binary_search(ALL(ans_string), now_string)) {
    ans++;
    ans_string.PB(now_string);
    SORT(ans_string);
    // DEBUG(now_string)
    return;
  }
  REP(i, can_use.size()) {
    string str = can_use[i];
    can_use.erase(can_use.begin() + i);
    solve(many_used + 1, now_string + str, can_use);
    can_use.PB(str);
    SORT(can_use);
  }
}
int main() {
  while (scanf("%d %d", &n, &k) && n && k) {
    REP(i, n) {
      scanf("%s", b);
      str_array.PB((string)b);
    }
    ans = 0;
    SORT(str_array);
    solve(0, "", str_array);
    printf("%d\\n", ans);
    str_array = VS();
    e = set<pair<string, VS>>();
    ans_string = VS();
  }
  return 0;
}