alexjercan/bugnet huggingface dataset:
Problem p01146 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, M, L, K, A, H, road[100][100], cold[100], res[100][100];
  while (cin >> N >> M >> L >> K >> A >> H, N || M || L || K || A) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        road[i][j] = res[i][j] = (i == j ? 0 : INF);
      }
    }
    for (int i = 0; i < L; i++) {
      cin >> cold[i];
    }
    cold[L] = A;
    cold[L + 1] = H;
    for (int i = 0, X, Y, T; i < K; i++) {
      cin >> X >> Y >> T;
      road[X][Y] = road[Y][X] = T;
    }
    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          road[i][j] = min(road[i][j], road[i][k] + road[k][j]);
        }
      }
    }
    for (int i = 0; i < L + 2; i++) {
      for (int j = 0; j < L + 2; j++) {
        if (road[cold[i]][cold[j]] <= M)
          res[i][j] = road[cold[i]][cold[j]];
      }
    }
    for (int k = 0; k < L + 2; k++) {
      for (int i = 0; i < L + 2; i++) {
        for (int j = 0; j < L + 2; j++) {
          res[i][j] = min(res[i][j], res[i][k] + res[k][j]);
        }
      }
    }
    if (res[L][L + 1] != INF) {
      cout << (res[L][L + 1] <= M ? res[L][L + 1] : res[L][L + 1] * 2 - M)
           << endl;
    } else {
      cout << "Help!" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define MAX 1000000000000
#define WM 10000000

using namespace std;

typedef long long int ll;

int M, N, L, H;

ll dfs(vector<bool> &flag, const vector<vector<int>> &road,
       const vector<int> &reito, int start) {
  if (road[start][H] <= M) {
    return road[start][H];
  }
  flag[start] = false;
  ll min = MAX, next;
  for (int i = 0; i < L; i++) {
    if (flag[reito[i]])
      if (road[start][reito[i]] <= M)
        if ((next = dfs(flag, road, reito, reito[i])) != -1)
          if (min > road[start][reito[i]] + next)
            min = road[start][reito[i]] + next;
  }
  flag[start] = true;
  if (min == MAX)
    return -1;
  else
    return min;
}

int main() {
  int K, A, X, Y, T;
  ll res;
  while (cin >> N >> M >> L >> K >> A >> H, N || M || L || K || A || H) {
    vector<int> reito(L);
    vector<bool> flag(N, true);
    vector<vector<int>> road(N, vector<int>(N, WM));
    for (int i = 0; i < N; i++) {
      road[i][i] = 0;
    }
    for (int i = 0; i < L; i++) {
      cin >> reito[i];
    }
    for (int i = 0; i < K; i++) {
      cin >> X >> Y >> T;
      road[X][Y] = road[Y][X] = T;
    }
    for (int k = 0; k < N; k++)
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          if ((road[i][k] != WM && road[k][j] != WM) &&
              (road[i][j] == WM || road[i][j] > road[i][k] + road[k][j]))
            road[i][j] = road[i][k] + road[k][j];
    res = dfs(flag, road, reito, A);
    if (res > M) {
      res = res * 2 - M;
    }
    if (res == -1) {
      cout << "Help!" << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}
Problem p01146 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
#define INF (1 << 26)
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

struct NODE {
  int to, cost, my;
  NODE(int a, int b, int c) { to = a, cost = b, my = c; }
};

bool operator<(const NODE &a, const NODE &b) { return a.cost > b.cost; }
int main() {
  int N, M, L, K, A, H;
  while (cin >> N >> M >> L >> K >> A >> H && N) {
    int ice[1001] = {0};
    int wf[101][101];
    rep(i, 101) rep(j, 101) wf[i][j] = (i != j ? INF : 0);
    ice[A] = ice[H] = true;
    rep(i, L) {
      int a;
      cin >> a;
      ice[a] = true;
    }
    rep(i, K) {
      int a, b, c;
      cin >> a >> b >> c;
      wf[a][b] = wf[b][a] = c;
    }
    rep(i, N) rep(j, N) rep(k, N) wf[j][k] = min(wf[j][k], wf[j][i] + wf[i][k]);

    rep(i, N) rep(j, N) if (!(ice[i] && ice[j] && wf[i][j] <= M)) wf[i][j] =
        INF;

    bool done[101] = {0};
    priority_queue<NODE> Q;
    Q.push(NODE(A, 0, M));
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (done[q.to])
        continue;
      else
        done[q.to] = true;
      if (q.to == H) {
        cout << q.cost << endl;
        goto exit;
      }
      for (int i = 0; i < N; i++) {
        if (ice[i]) {
          if (wf[q.to][i] != INF)
            Q.push(NODE(i, q.cost + wf[q.to][i] + max(0, wf[q.to][i] - q.my),
                        max(0, q.my - wf[q.to][i])));
        }
      }
    }
    cout << "Help!" << endl;
  exit:;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, pair<int, int>> pii;

struct NODE {
  int to, remain, cost;
  NODE(int a, int b, int c) { to = a, remain = b, cost = c; }
};
bool operator<(const NODE &a, const NODE &b) { return a.cost > b.cost; }
int main() {
  int n, m, l, k, a, h;
  while (cin >> n >> m >> l >> k >> a >> h && n) {
    vector<vector<NODE>> g(n);
    int fz[101] = {0};
    for (int i = 0; i < l; i++) {
      int w;
      cin >> w;
      fz[w] = true;
    }

    for (int i = 0; i < k; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      g[x].push_back(NODE(y, 0, z));
      g[y].push_back(NODE(x, 0, z));
    }
    bool done[101][101] = {0};
    priority_queue<NODE> Q;
    Q.push(NODE(a, m, 0));
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (done[q.to][q.remain])
        continue;
      else
        done[q.to][q.remain] = true;
      if (q.to == h) {
        cout << q.cost << endl;
        goto end;
      }
      if (fz[q.to]) {
        for (int i = 0; i + q.remain <= m; i++) {
          if (!done[q.to][i + q.remain])
            Q.push(NODE(q.to, i + q.remain, i + q.cost));
        }
      }
      for (int i = 0; i < g[q.to].size(); i++) {
        if (q.remain < g[q.to][i].cost)
          continue;
        if (!done[g[q.to][i].to][q.remain - g[q.to][i].cost])
          Q.push(NODE(g[q.to][i].to, q.remain - g[q.to][i].cost,
                      q.cost + g[q.to][i].cost));
      }
    }
    cout << "Help!" << endl;
  end:;
  }
}
Problem p01147 (C++) — buggy submission, verdict: Runtime Error

// 44
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int d[10][10];
int n;
string ms;
int ml;
string s[10];

void dfs(int l, int sz, int u, vector<int> v) {
  if (u == (1 << n) - 1) {
    string t;
    if (sz <= ml) {
      t = s[v[0]];
      for (int i = 1; i < v.size(); i++) {
        t += s[v[i]].substr(s[v[i]].size() - d[v[i - 1]][v[i]]);
      }
    }
    if (sz < ml || sz == ml && t < ms) {
      ml = sz;
      ms = t;
    }
  } else {
    v.push_back(0);
    for (int i = 0; i < n; i++) {
      if (!(u >> i & 1)) {
        v.back() = i;
        dfs(i, sz + d[l][i], u | 1 << i, v);
      }
    }
  }
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    int u = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i != j) {
          if (!(u >> i & 1) && s[i].find(s[j]) != string::npos) {
            u |= 1 << j;
          } else {
            int k;
            for (k = min(s[i].size(), s[j].size());; k--) {
              if (s[i].compare(s[i].size() - k, k, s[j], 0, k) == 0)
                break;
            }
            d[i][j] = s[j].size() - k;
          }
        }
      }
    }
    ml = 1 << 30;
    for (int i = 0; i < n; i++) {
      dfs(i, s[i].size(), u | 1 << i, vector<int>(1, i));
    }
    cout << ms << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

// 44
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int d[10][10];
int n;
string ms;
int ml;
string s[10];

void dfs(int l, int sz, int u, vector<int> v) {
  if (u == (1 << n) - 1) {
    string t;
    if (sz <= ml) {
      t = s[v[0]];
      for (int i = 1; i < v.size(); i++) {
        t += s[v[i]].substr(s[v[i]].size() - d[v[i - 1]][v[i]]);
      }
    }
    if (sz < ml || sz == ml && t < ms) {
      ml = sz;
      ms = t;
    }
  } else {
    v.push_back(0);
    for (int i = 0; i < n; i++) {
      if (!(u >> i & 1)) {
        v.back() = i;
        dfs(i, sz + d[l][i], u | 1 << i, v);
      }
    }
  }
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    int u = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i != j) {
          if (search(s[i].begin(), s[i].end(), s[j].begin(), s[j].end()) !=
              s[i].end()) {
            u |= 1 << j;
          } else {
            int k;
            for (k = min(s[i].size(), s[j].size());; k--) {
              if (s[i].substr(s[i].size() - k) == s[j].substr(0, k))
                break;
            }
            d[i][j] = s[j].size() - k;
          }
        }
      }
    }
    ml = 1 << 30;
    for (int i = 0; i < n; i++) {
      dfs(i, s[i].size(), 1 << i, vector<int>(1, i));
    }
    cout << ms << endl;
  }
  return 0;
}
Problem p01147 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define REP(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) REP(i, 0, n)
int g[10][10];
string s[10];
string dpv[10][1 << 10];
bool dp[10][1 << 10];
int n;
string mini(const string &a, const string &b) {
  if (a.size() == b.size())
    return min(a, b);
  return a.size() < b.size() ? a : b;
}
string solve(int v, int u) {
  if (u == (1 << n) - 1)
    return "";
  if (v >= 0 && dp[v][u]) {
    return dpv[v][u];
  }
  string ret;
  bool f = false;
  if (v >= 0)
    dp[v][u] = true;
  rep(i, n) {
    if (u >> i & 1)
      continue;
    string ad;
    if (v < 0) {
      ad = s[i];
    } else {
      ad = s[i].substr(g[i][v]);
    }
    if (f)
      ret = mini(ret, ad + solve(i, u | 1 << i));
    else {
      ret = ad + solve(i, u | 1 << i);
      f = true;
    }
  }
  return v >= 0 ? dpv[v][u] = ret : ret;
}
int main(void) {
  for (;;) {
    scanf("%d", &n);
    if (!n)
      break;
    rep(i, n) cin >> s[i];
    int u = 0;
    rep(i, n) rep(j, n) {
      if (u & 1 << j || u & 1 << i || i == j)
        continue;
      if (s[i].find(s[j]) != -1) {
        u |= 1 << j;
      }
    }
    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      if (u & 1 << i || u & 1 << j)
        continue;
      int c = 1, cc = -1;
      for (int ii = s[i].size() - 1; ii >= 0; ii--) {
        if (s[j].size() < c)
          break;
        int jj;
        for (jj = 0; jj < c; jj++) {
          if (s[i][ii + jj] != s[j][jj])
            break;
        }
        if (jj == c) {
          cc = c;
        }
        c++;
      }
      g[j][i] = cc < 0 ? 0 : cc;
    }
    memset(dp, 0, sizeof(dp));
    puts(solve(-1, u).data());
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void solve() {
  int N;
  while (cin >> N, N) {
    vector<string> Vec(N);
    vector<int> num(N);
    for (int i = 0; i < N; ++i) {
      cin >> Vec[i];
      num[i] = i;
    }

    string Answer;
    for (int i = 0; i < 1000; ++i) {
      Answer += "a";
    }
    do {
      string result = Vec[num[0]];
      for (int i = 0; i < N - 1; ++i) {
        int pos = 0;
        int count = 0;
        for (int j = 0; j < Vec[num[i]].size(); ++j) {
          if (Vec[num[i]][j] == Vec[num[i + 1]][pos]) {
            count++;
            pos++;
          } else {
            count = 0;
            pos = 0;
            if (Vec[num[i]][j] == Vec[num[i + 1]][pos]) {
              count++;
              pos++;
            }
          }
        }
        for (int j = count; j < Vec[num[i + 1]].size(); ++j) {
          result += Vec[num[i + 1]][j];
        }
      }
      if (result.size() < Answer.size()) {
        Answer.clear();
        Answer += result;
      }
    } while (next_permutation(num.begin(), num.end()));
    cout << Answer << endl;
  }
}

int main() {
  solve();
  return (0);
}
Problem p01147 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int n;
string v[10];
map<string, string> memo[1024];

string merge(string a, string b) {
  if (~a.find(b))
    return a;
  for (int i = 0; i < a.size(); i++) {
    bool f = true;
    for (int j = 0; i + j < a.size(); j++) {
      if (a[i + j] != b[j]) {
        f = false;
        break;
      }
    }
    if (f)
      return a.substr(0, i) + b;
  }
  return a + b;
}

int main() {
  // cout << merge("abc","aab") << endl;
  while (cin >> n && n) {
    for (int i = 0; i < 1024; i++)
      memo[i].clear();
    for (int i = 0; i < n; i++)
      cin >> v[i];
    sort(v, v + n);
    for (int i = 0; i < n; i++) {
      memo[1 << i][v[i]] = v[i];
    }
    for (int bit = 0; bit < (1 << n); bit++) {
      for (map<string, string>::iterator it = memo[bit].begin();
           it != memo[bit].end(); ++it) {
        string s = it->second;
        for (int i = 0; i < n; i++) {
          if (!(bit >> i & 1)) {
            string m = merge(s, v[i]);
            string sub = m.substr(max(0, (int)m.size() - 10));
            string &ans = memo[bit | (1 << i)][sub];

            if (ans == "" || ans.length() > m.length() ||
                (ans.length() == m.length() && m < ans))
              ans = m;
          }
        }
      }
    }
    string ans = "";
    for (map<string, string>::iterator it = memo[(1 << n) - 1].begin();
         it != memo[(1 << n) - 1].end(); ++it) {
      string m = it->second;
      if (ans == "" || ans.length() > m.length() ||
          (ans.length() == m.length() && m < ans))
        ans = m;
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

int n;
string v[10];
map<string, string> memo[1024];

string merge(string a, string b) {
  if (~a.find(b))
    return a;
  for (int i = 0; i < a.size(); i++) {
    bool f = true;
    for (int j = 0; i + j < a.size(); j++) {
      if (a[i + j] != b[j]) {
        f = false;
        break;
      }
    }
    if (f)
      return a.substr(0, i) + b;
  }
  return a + b;
}
string f(int bit, string s) {
  string sub = s;
  if (memo[bit].count(sub))
    return memo[bit][sub];
  if (bit == 0)
    return s;
  string ans = "";
  for (int i = 0; i < n; i++) {
    if (bit >> i & 1) {
      string r = f(bit & ~(1 << i), merge(s, v[i]));
      if (ans == "" || ans.length() > r.length() ||
          (ans.length() == r.length() && r < ans))
        ans = r;
    }
  }
  return memo[bit][sub] = ans;
}
int main() {
  while (cin >> n && n) {
    for (int i = 0; i < 1024; i++)
      memo[i].clear();
    for (int i = 0; i < n; i++)
      cin >> v[i];
    sort(v, v + n);

    cout << f((1 << n) - 1, "") << endl;
  }
}
Problem p01147 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)

int N;
vector<string> dp;

string merge(string s, string t) {
  if (s.find(t) != string::npos)
    return s;
  for (int i = max(0, (int)(s.size() - t.size())); i < (int)s.size(); ++i) {
    if (s.substr(i) == t.substr(0, (int)s.size() - i)) {
      return s.substr(0, i) + t;
    }
  }
  return s + t;
}

string solve() {
  rep(S, 1 << N) {
    if (__builtin_popcount(S) == 1)
      continue;
    for (int U = S - 1 & S; U > 0; --U &= S) {
      dp[S] = min(
          dp[S], merge(dp[U], dp[S ^ U]), [](const string &s, const string &t) {
            return s.size() < t.size() || (s.size() == t.size() && s < t);
          });
    }
  }
  return dp[(1 << N) - 1];
}

int main() {
  while (cin >> N, N) {
    dp.assign(1 << N, string(100, 'z'));
    rep(i, N) cin >> dp[1 << i];
    cout << solve() << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(c) (c).begin(), (c).end()

int N;
vector<string> str;

void merge(string &s, string t) {
  for (int i = max(0, (int)(s.size() - t.size())); (int)s.size(); ++i) {
    if (s.substr(i) == t.substr(0, (int)s.size() - i)) {
      s += t.substr((int)s.size() - i);
      return;
    }
  }
  s += t;
}

string solve() {
  string res(100, 'z');
  do {
    string SSS = str[0];
    rep(i, N) {
      if (res.size() < SSS.size())
        break;
      if (SSS.find(str[i]) != string::npos)
        continue;
      else
        merge(SSS, str[i]);
    }
    if (SSS.size() < res.size() || (SSS.size() == res.size() && SSS < res))
      res = SSS;
  } while (next_permutation(all(str)));
  return res;
}

int main() {
  while (cin >> N, N) {
    str.assign(N, "");
    rep(i, N) cin >> str[i];
    cout << solve() << '\\n';
  }
  return 0;
}
Problem p01147 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
vector<string> uvs;
pair<int, string> solve(void) {
  string answer = "";
  for (string &s : uvs) {
    int ol = min(answer.size(), s.size());
    for (; ol > 0; --ol) {
      bool flag = true;
      REP(i, ol) if (answer[answer.size() - ol + i] != s[i]) {
        flag = false;
        break;
      }
      if (flag)
        break;
    }
    for (auto i = s.begin() + ol; i != s.end(); ++i)
      answer.push_back(*i);
  }
  return make_pair(answer.size(), answer);
}
int main(void) {
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    vector<pair<int, string>> vs(n);
    uvs.clear();
    REP(i, n) {
      string s;
      cin >> s;
      vs[i] = make_pair(-(int)s.size(), s);
    }
    SORT(vs);
    REP(i, n) {
      bool flag = true;
      REP(j, i) REP(k, vs[i].first - vs[j].first) {
        string tmp = vs[j].second;
        string r(tmp.begin() + k, tmp.begin() + k - vs[i].first);
        if (vs[i].second == r)
          flag = false;
      }
      if (flag)
        uvs.push_back(vs[i].second);
    }
    SORT(uvs);
    pair<int, string> answer(1000, "");
    do {
      answer = min(answer, solve());
    } while (next_permutation(uvs.begin(), uvs.end()));
    cout << answer.second << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
vector<string> uvs;
pair<int, string> solve(void) {
  string answer = "";
  for (string &s : uvs) {
    int ol = min(answer.size(), s.size());
    for (; ol > 0; --ol) {
      bool flag = true;
      REP(i, ol) if (answer[answer.size() - ol + i] != s[i]) {
        flag = false;
        break;
      }
      if (flag)
        break;
    }
    answer += string(s.begin() + ol, s.end());
  }
  return make_pair(answer.size(), answer);
}
int main(void) {
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    vector<pair<int, string>> vs(n);
    uvs.clear();
    REP(i, n) {
      string s;
      cin >> s;
      vs[i] = make_pair(-(int)s.size(), s);
    }
    SORT(vs);
    REP(i, n) {
      bool flag = true;
      REP(j, i) REP(k, vs[i].first - vs[j].first) {
        string tmp = vs[j].second;
        string r(tmp.begin() + k, tmp.begin() + k - vs[i].first);
        if (vs[i].second == r)
          flag = false;
      }
      if (flag)
        uvs.push_back(vs[i].second);
    }
    SORT(uvs);
    pair<int, string> answer(1000, "");
    do {
      answer = min(answer, solve());
    } while (next_permutation(uvs.begin(), uvs.end()));
    cout << answer.second << endl;
  }
}
Problem p01147 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int n;
string str[10];
int t[10][10];
string u[10][10];
int v[10];
int w[10];

string ans, tmp;
int ans_size, tmp_size;
int count(string &, string &, int, int);
int main() {
  while (cin >> n && n) {
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      for (int j = 0; j < i; j++) {
        t[i][j] = count(str[i], str[j], i, j);
        t[j][i] = count(str[j], str[i], j, i);
      }
    }
    ans_size = (1 << 24);
    for (int i = 0; i < n; i++)
      w[i] = i;
    do {
      for (int i = 0; i < n; i++)
        v[i] = w[i];
      tmp_size = str[v[0]].size();
      for (int i = 1; i < n; i++) {
        tmp_size += t[v[i - 1]][v[i]];
        if (t[v[i - 1]][v[i]] == 0)
          v[i] = v[i - 1];
      }
      if (tmp_size < ans_size) {
        ans_size = tmp_size;
        ans = str[v[0]];
        for (int i = 1; i < n; i++)
          ans += u[v[i - 1]][v[i]];
      } else if (tmp_size == ans_size) {
        tmp = str[v[0]];
        for (int i = 1; i < n; i++)
          tmp += u[v[i - 1]][v[i]];
        if (tmp < ans)
          ans = tmp;
      }
    } while (next_permutation(w, w + n));
    cout << ans << endl;
  }
  return 0;
}

int count(string &a, string &b, int I, int J) {
  int i, j, c = a.size(), d = b.size();
  bool flg;
  for (i = 0; i < c; i++) {
    flg = true;
    for (j = 0; (j < d && i + j < c); j++)
      if (a[i + j] != b[j])
        flg = false;
    if (flg) {
      if (j < d)
        u[I][J] = b.substr(j);
      else
        u[I][J] = "";
      return u[I][J].size();
    }
  }
  u[I][J] = b;
  return d;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int n;
string str[10];
int t[10][10];
string u[10][10];

int v[10];
string ans, tmp;

int count(string &, string &, int, int);
int main() {
  while (cin >> n && n) {
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      for (int j = 0; j < i; j++) {
        t[i][j] = count(str[i], str[j], i, j);
        t[j][i] = count(str[j], str[i], j, i);
        // cout<<str[i]<<"==="<<str[j]<<endl;
        // cout<<u[i][j]<<" --- "<<u[j][i]<<endl;
      }
    }
    ans = "";
    for (int i = 0; i < n; i++)
      v[i] = i;
    do {
      tmp = str[v[0]];
      for (int i = 1; i < n; i++) {
        tmp += u[v[i - 1]][v[i]];
      }
      if (ans.size() == 0 || tmp.size() < ans.size())
        ans = tmp;
      else if (tmp.size() == ans.size() && tmp < ans)
        ans = tmp;
    } while (next_permutation(v, v + n));
    cout << ans << endl;
  }
  return 0;
}

int count(string &a, string &b, int I, int J) {
  int i, j, c = a.size(), d = b.size();
  // cout<<a<<' '<<b<<endl;
  bool flg;
  for (i = 0; i < c; i++) {
    flg = true;
    for (j = 0; (j < d && i + j < c); j++) {
      if (a[i + j] != b[j])
        flg = false;
    }
    if (flg) {
      if (j > 0)
        u[I][J] = b.substr(j);
      else
        u[I][J] = "";
      return u[I][J].size();
    }
  }
  u[I][J] = b;
  return d;
}