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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool match(string &s1, string &s2) {
  if (s1.size() < s2.size())
    return false;
  int idx = 0;
  for (int i = 0; i <= s1.size() - s2.size(); i++) {
    if (s1[i] == s2[0]) {
      bool f = true;
      for (int j = 0; j < s2.size(); j++) {
        if (s1[i + j] != s2[j]) {
          f = false;
          break;
        }
      }
      if (f)
        return true;
    }
  }
  return false;
}

int main() {

  int n;
  while (cin >> n && n != 0) {
    vector<string> ss;
    vector<string> tmp;
    int a[11];
    int matchSize[11][11];
    string matchStr[11][11];

    memset(matchSize, 0, sizeof(matchSize));
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      tmp.push_back(s);
    }
    bool ok[21];
    fill(ok, ok + 21, true);
    for (int i = 0; i < n; i++) {
      if (!ok[i])
        continue;
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        if (match(tmp[i], tmp[j])) {
          ok[j] = false;
        }
      }
    }
    for (int i = 0; i < n; i++)
      if (ok[i])
        ss.push_back(tmp[i]);
    n = ss.size();
    for (int i = 0; i < ss.size(); i++)
      a[i] = i;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        matchStr[i][j] = ss[j];
        for (int k = min(ss[i].size(), ss[j].size()); k >= 1; k--) {
          if (ss[i].substr(ss[i].size() - k, k) == ss[j].substr(0, k)) {
            matchSize[i][j] = k;
            matchStr[i][j] = ss[j].substr(k);
            break;
          }
        }
      }
    }
    int maxSize = 1000000;
    string res;
    do {
      int sumSize = ss[a[0]].size();
      for (int i = 1; i < n; i++)
        sumSize += ss[a[i]].size() - matchSize[a[i - 1]][a[i]];
      if (maxSize > sumSize) {
        maxSize = sumSize;
        // resツづーツ青カツ青ャ
        string s = ss[a[0]];
        for (int i = 1; i < n; i++)
          s += matchStr[a[i - 1]][a[i]];
        res = s;
      } else if (maxSize == sumSize) {
        string s = ss[a[0]];
        for (int i = 1; i < n; i++)
          s += matchStr[a[i - 1]][a[i]];
        if (res > s)
          res = s;
      }
    } while (next_permutation(a, a + n));
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

// ñÂÌ¶ñªïÜÖWÉ é©
bool match(string &s1, string &s2) {
  if (s1 == "abcd" && s2 == "bc") {
    //    cout<<"ok"<<endl;
  }
  if (s1.size() < s2.size())
    return false;
  for (int i = 0; i <= s1.size() - s2.size(); i++) {
    if (s1[i] == s2[0]) {
      bool f = false;
      for (int j = 0; j < s2.size(); j++) {
        if (s1[i + j] == s2[j])
          continue;
        else {
          f = true;
          break;
        }
      }
      if (!f)
        return true;
    }
  }
  return false;
}

int main() {

  int n;
  while (cin >> n && n != 0) {
    vector<string> ss;
    int a[11];
    int matchSize[11][11];
    string matchStr[11][11];

    memset(matchSize, 0, sizeof(matchSize));
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      ss.push_back(s);
      a[i] = i;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        matchStr[i][j] = ss[j];
        for (int k = min(ss[i].size(), ss[j].size()); k >= 1; k--) {
          if (ss[i].substr(ss[i].size() - k, k) == ss[j].substr(0, k)) {
            matchSize[i][j] = k;
            matchStr[i][j] = ss[j].substr(k);
            break;
          }
        }
      }
    }
    int maxSize = 1000000;
    string res;
    // cout<<"start"<<endl;
    do {
      int sumSize = ss[a[0]].size();
      string s = ss[a[0]];
      for (int i = 1; i < n; i++) {
        if (!match(s, ss[a[i]])) {
          s += matchStr[a[i - 1]][a[i]];
        }
      }
      if (res.size() == 0 || s.size() < res.size())
        res = s;
      else if (s.size() == res.size()) {
        if (s < res)
          res = s;
      }
    } while (next_permutation(a, a + n));
    // cout<<"fin"<<endl;
    cout << res << endl;
  }

  return 0;
}