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

#include <bits/stdc++.h>

using namespace std;
using LL = long long;

constexpr int MAXN = 55;

struct Node {
  Node() = default;
  Node(LL _c, string &_l, string &_r) : c(_c), l(_l), r(_r) {}
  LL c;
  string l, r;
  friend inline bool operator<(const Node &lhs, const Node &rhs) {
    return lhs.c > rhs.c;
  }
};
LL n, cost[MAXN];
string s[MAXN], emps("");

inline bool isPalindrome(const string &tmp) {
  for (int i = 0; i < tmp.size() / 2 + 1; i++)
    if (tmp[i] != tmp[tmp.size() - 1 - i])
      return false;
  return true;
}
/*@return l, r第一个不同的位置*/
inline int check(string &l, string &r) {
  int i = 0;
  for (; i < min(l.size(), r.size()); i++)
    if (l[i] != r[r.size() - 1 - i])
      break;
  l = l.substr(i), r = r.substr(0, r.size() - i);
  return i;
}

priority_queue<Node, vector<Node>> q;
map<string, map<string, bool>> vis;
inline void search() {
  while (!q.empty()) {
    auto cur = q.top();
    q.pop();
    if (vis[cur.l][cur.r])
      continue;
    if (isPalindrome(cur.l) && cur.r.empty()) {
      cout << cur.c << endl;
      return;
    } else if (isPalindrome(cur.r) && cur.l.empty()) {
      cout << cur.c << endl;
      return;
    } else if (cur.l.empty() && cur.r.empty()) {
      cout << cur.c << endl;
      return;
    } else {
      for (int i = 1; i <= n; i++) {
        Node tmp(cur.c, cur.l, cur.r);
        if (tmp.l.size() >= tmp.r.size()) {
          tmp.r = s[i] + tmp.r, tmp.c += cost[i];
          int id = check(tmp.l, tmp.r);
          if (id && (tmp.l.empty() || tmp.r.empty()))
            q.push(tmp);
        } else {
          tmp.l = tmp.l + s[i], tmp.c += cost[i];
          int id = check(tmp.l, tmp.r);
          if (id && (tmp.l.empty() || tmp.r.empty()))
            q.push(tmp);
        }
      }
    }
    vis[cur.l][cur.r] = true;
  }
  cout << -1 << endl;
}

int main() {
  // freopen("test.in", "r", stdin);
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i] >> cost[i];
    q.push(Node(cost[i], s[i], emps));
    q.push(Node(cost[i], emps, s[i]));
  }
  search();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using LL = long long;

constexpr int MAXN = 55;

struct Node {
  Node() = default;
  Node(LL _c, string &_l, string &_r) : c(_c), l(_l), r(_r) {}
  LL c;
  string l, r;
  friend inline bool operator<(const Node &lhs, const Node &rhs) {
    return lhs.c > rhs.c;
  }
};
LL n, cost[MAXN];
string s[MAXN], emps("");
time_t st;

inline bool isPalindrome(const string &tmp) {
  for (int i = 0; i < tmp.size() / 2 + 1; i++)
    if (tmp[i] != tmp[tmp.size() - 1 - i])
      return false;
  return true;
}
/*@return l, r第一个不同的位置*/
inline int check(string &l, string &r) {
  int i = 0;
  for (; i < min(l.size(), r.size()); i++)
    if (l[i] != r[r.size() - 1 - i])
      break;
  l = l.substr(i), r = r.substr(0, r.size() - i);
  return i;
}

priority_queue<Node, vector<Node>> q;
map<string, map<string, bool>> vis;
inline void search() {
  while (!q.empty()) {
    auto cur = q.top();
    q.pop();
    if (vis[cur.l][cur.r])
      continue;
    if (isPalindrome(cur.l) && cur.r.empty()) {
      cout << cur.c << endl;
      return;
    } else if (isPalindrome(cur.r) && cur.l.empty()) {
      cout << cur.c << endl;
      return;
    } else if (cur.l.empty() && cur.r.empty()) {
      cout << cur.c << endl;
      return;
    } else {
      for (int i = 1; i <= n; i++) {
        Node tmp(cur.c, cur.l, cur.r);
        if (tmp.l.size() >= tmp.r.size()) {
          tmp.r = s[i] + tmp.r;
          tmp.c += cost[i];
          // cout << tmp.l << " " << tmp.r << endl;
          int id = check(tmp.l, tmp.r);
          if (id)
            q.push(tmp);
        } else {
          tmp.l = tmp.l + s[i];
          tmp.c += cost[i];
          // cout << tmp.l << " " << tmp.r << endl;
          int id = check(tmp.l, tmp.r);
          if (id)
            q.push(tmp);
        }
      }
    }
  }
  cout << -1 << endl;
}

int main() {
  // freopen("test.in", "r", stdin);
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  st = clock();
  for (int i = 1; i <= n; i++) {
    cin >> s[i] >> cost[i];
    q.push(Node(cost[i], s[i], emps));
    q.push(Node(cost[i], emps, s[i]));
  }
  search();
  return 0;
}