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


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

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

int n;
vector<string> in;

string connect(string &a, string &b) {
  for (int i = max(a.size(), b.size()); i >= 1; i--) {
    bool ok = true;
    for (int j = 0; j < i; j++) {
      if (a[a.size() - i + j] != b[j])
        ok = false;
    }
    if (ok)
      return a + b.substr(i);
  }
  return a + b;
}

bool input() {
  cin >> n;
  if (n == 0)
    return false;
  in.clear();
  rep(i, n) {
    string a;
    cin >> a;
    in.push_back(a);
  }

  for (int i = 0; i < in.size(); i++) {
    for (int j = 0; j < in.size(); j++) {
      if (i == j)
        continue;
      if (in[j].find(in[i]) != -1) {
        in.erase(in.begin() + i);
        i--;
        break;
      }
    }
  }

  // rep(i,in.size())cout<<in[i]<<endl;

  return true;
}

string small(string &a, string &b) {
  if (a == "")
    return b;
  if (b == "")
    return a;
  if (a.size() == b.size())
    return min(a, b);
  if (a.size() < b.size())
    return a;
  return b;
}

const int N = 10;

string memo[N + 1][(1 << N)];

string gbit(int n, int state) {
  string s = "";
  rep(i, n) {
    if ((state & (1 << i)) > 0)
      s += "1";
    else
      s += "0";
  }
  reverse(s.begin(), s.end());
  return s;
}

string saiki(int pos, int state) {
  if (memo[pos][state] != "") {
    return memo[pos][state];
  }

  string ans = "";
  for (int i = 0; i < in.size(); i++) {
    if ((state & (1 << i)) == 0) {
      string s1 = saiki(i, state | (1 << i));
      string s2 = connect(s1, in[pos]);
      ans = small(ans, s2);
    }
  }

  if (ans == "")
    ans = in[pos];

  // cout<<pos<<" "<<gbit(in.size(),state)<<" "<<ans<<endl;

  return memo[pos][state] = ans;
}

string solve() {
  rep(i, N) rep(j, (1 << N)) memo[i][j] = "";

  string ans = "";
  for (int i = 0; i < in.size(); i++) {
    string s = saiki(i, (1 << i));
    ans = small(ans, s);
  }
  return ans;
}

int main() {
  while (input())
    cout << solve() << endl;
}

Error: TLE

Fixed submission:


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

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

const int N = 11;

int n;
vector<string> in;

bool input() {
  cin >> n;
  if (n == 0)
    return false;
  in.clear();
  rep(i, n) {
    string a;
    cin >> a;
    in.push_back(a);
  }
  return true;
}

string ans;
void update(string &a) {
  if (ans == "")
    ans = a;
  else if (ans.size() > a.size())
    ans = a;
  else if (ans.size() == a.size() && ans > a)
    ans = a;
}

string connect(string &a, string &b) {
  for (int len = min(a.size(), b.size()); len >= 1; len--) {
    bool ok = true;
    rep(j, len) {
      if (a[a.size() - len + j] != b[j])
        ok = false;
    }
    if (ok) {
      return a + b.substr(len);
    }
  }
  return a + b;
}

int ind[N];
int match[N][N];
void saiki(int depth, string rs) {

  // cout<<depth<<" "<<rs<<endl;
  if (depth == n) {
    update(rs);
    return;
  }

  rep(i, n) {
    bool ok = true;
    rep(j, depth) {
      if (ind[j] == i)
        ok = false;
    }
    if (ok) {
      string s = connect(rs, in[i]);
      if (depth == 0)
        s = in[i];
      if (s.size() != 0) {
        ind[depth] = i;
        saiki(depth + 1, s);
      }
    }
  }
}

string solve() {
  ans = "";

  saiki(0, "");
  return ans;
}

int main() {
  while (input())
    cout << solve() << endl;
}