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

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

int N;
string names[14];
string use[14];
bool flg[14];
int dp[1 << 14][14];
int sc[20][20];

int samecnt(int s1, int s2) {
  int ret = 0;
  int len = min(use[s1].size(), use[s2].size());
  for (int j = 0; j < len; j++) {
    bool flg = true;
    for (int i = 0; i < j; i++) {
      if (use[s1][use[s1].size() - j + i] != use[s2][i]) {
        flg = false;
      }
    }
    if (flg)
      ret = j;
  }
  return ret;
}

int main() {
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      cin >> names[i];
      flg[i] = true;
    }
    int idx = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          continue;
        if (names[i].find(names[j]) != string::npos) {
          flg[j] = false;
        }
      }
    }
    for (int i = 0; i < N; i++) {
      if (flg[i]) {
        use[idx++] = names[i];
      }
    }
    for (int i = 0; i < idx; i++) {
      for (int j = 0; j < idx; j++) {
        sc[i][j] = samecnt(i, j);
      }
    }
    for (int i = 0; i < (1 << idx); i++) {
      for (int j = 0; j < idx; j++) {
        int val = 10000000;
        int mask = 1 << j;
        if ((i & mask) == 0) {

        } else {
          int m2 = i & (~mask);
          if (m2 == 0) {
            val = use[j].size();
          } else {
            for (int k = 0; k < idx; k++) {
              int np = dp[m2][k] + use[j].size() - sc[k][j];
              val = min(val, np);
            }
          }
        }
        dp[i][j] = val;
      }
    }
    /*
                    for(int i = 0; i < (1 << idx); i++){
                            cout << static_cast<std::bitset<8> >(i);
                            for(int j = 0; j < idx; j++){
                                    cout << " " << dp[i][j];
                            }
                            cout << endl;
                    }
    */
    int ans = 10000000;
    for (int i = 0; i < idx; i++) {
      ans = min(dp[(1 << idx) - 1][i], ans);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int N;
string names[14];
string use[14];
bool flg[14];
int dp[1 << 14][14];

int samecnt(int s1, int s2) {
  int ret = 0;
  int len = min(use[s1].size(), use[s2].size());
  for (int j = 0; j < len; j++) {
    bool flg = true;
    for (int i = 0; i < j; i++) {
      if (use[s1][use[s1].size() - j + i] != use[s2][i]) {
        flg = false;
      }
    }
    if (flg)
      ret = j;
  }
  return ret;
}

int main() {
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      cin >> names[i];
      flg[i] = true;
    }
    int idx = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          continue;
        if (names[i].find(names[j]) != string::npos) {
          flg[j] = false;
        }
      }
    }
    for (int i = 0; i < N; i++) {
      if (flg[i]) {
        use[idx++] = names[i];
      }
    }
    for (int i = 0; i < (1 << idx); i++) {
      for (int j = 0; j < idx; j++) {
        int val = 10000000;
        int mask = 1 << j;
        if ((i & mask) == 0) {

        } else {
          int m2 = i & (~mask);
          if (m2 == 0) {
            val = use[j].size();
          } else {
            for (int k = 0; k < idx; k++) {
              int np = dp[m2][k] + use[j].size() - samecnt(k, j);
              val = min(val, np);
            }
          }
        }
        dp[i][j] = val;
      }
    }
    /*
                    for(int i = 0; i < (1 << idx); i++){
                            cout << static_cast<std::bitset<8> >(i);
                            for(int j = 0; j < idx; j++){
                                    cout << " " << dp[i][j];
                            }
                            cout << endl;
                    }
    */
    int ans = 10000000;
    for (int i = 0; i < idx; i++) {
      ans = min(dp[(1 << idx) - 1][i], ans);
    }
    cout << ans << endl;
  }
}