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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;

typedef pair<string, int> P;

int n;
string str[11];
string INF;

map<P, string> m;

int check(string s) {
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if ((int)s.find(str[i]) != -1) {
      ret |= 1 << i;
    }
  }
  return ret;
}

string solve(string sss, int used) {
  if (m[P(sss, used)] != "")
    return m[P(sss, used)];

  if (used == (1 << n) - 1)
    return "";

  string ret = INF;
  string temp = "";
  int c;

  for (int i = 0; i < n; i++) {
    if ((used & 1 << i) == 0) {
      /*
        if((int)sss.find(str[i]) != -1){
        //ret = min(ret, solve(sss,used|1<<i));
        temp = sss + solve(sss,used|1<<i);
        ret = (ret.size() < temp.size())?ret:temp;
        }else{
      */
      for (int j = 1; j < (int)(min(sss.size(), str[i].size())); j++) {
        if (sss.substr(sss.size() - j) == str[i].substr(0, j)) {
          // ret = min(ret, solve(sss + str[i].substr(j),used|1<<i) +
          // (int)str[i].size() - j);

          string t = sss + str[i].substr(j);
          c = check(t);
          temp = str[i].substr(j) + solve(t.substr(max((int)t.size() - 10, 0)),
                                          (used | 1 << i) | c);
          if (ret.size() == temp.size()) {
            ret = (ret < temp) ? ret : temp;
          } else {
            ret = (ret.size() < temp.size()) ? ret : temp;
          }
        }
      }
      // ret = min(ret, solve(sss + str[i],used|1<<i) + (int)str[i].size());

      string t = sss + str[i];
      c = check(t);
      temp = str[i] +
             solve(t.substr(max((int)t.size() - 10, 0)), (used | 1 << i) | c);
      if (ret.size() == temp.size()) {
        ret = (ret < temp) ? ret : temp;
      } else {
        ret = (ret.size() < temp.size()) ? ret : temp;
      }
      //}
    }
  }
  // cout<< ret << endl;
  return m[P(sss, used)] = ret;
  // return m[P(sss,used)] = ret.substr((int)sss.size());
}

int main(void) {
  for (int i = 0; i < 111; i++)
    INF += "A";

  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      cin >> str[i];
    }
    map<P, string> a;
    m = a;

    cout << solve("", 0) << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;

typedef pair<string, int> P;

int n;
string str[11];
string INF;

map<P, string> m;

string solve(string sss, int used) {
  if (m[P(sss, used)] != "")
    return m[P(sss, used)];

  if (used == (1 << n) - 1)
    return sss;

  string ret = INF;
  string temp = "";

  for (int i = 0; i < n; i++) {
    if ((used & 1 << i) == 0) {
      if ((int)sss.find(str[i]) != -1) {
        // ret = min(ret, solve(sss,used|1<<i));
        temp = sss + solve(sss, used | 1 << i);
        ret = (ret.size() < temp.size()) ? ret : temp;
      } else {
        for (int j = 1; j < (int)(min(sss.size(), str[i].size())); j++) {
          if (sss.substr(sss.size() - j) == str[i].substr(0, j)) {
            // ret = min(ret, solve(sss + str[i].substr(j),used|1<<i) +
            // (int)str[i].size() - j);
            temp = sss + solve(sss + str[i].substr(j), used | 1 << i);
            ret = (ret.size() < temp.size()) ? ret : temp;
          }
        }
        // ret = min(ret, solve(sss + str[i],used|1<<i) + (int)str[i].size());
        temp = sss + solve(sss + str[i], used | 1 << i);
        ret = (ret.size() < temp.size()) ? ret : temp;
      }
    }
  }
  return m[P(sss, used)] = ret.substr((int)sss.size());
}

int main(void) {
  for (int i = 0; i < 111; i++)
    INF += "A";

  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      cin >> str[i];
    }
    map<P, string> a;
    m = a;

    cout << solve("", 0) << endl;
  }
}