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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod

ll N;
struct St {
  ll val;
  string str;
  ll pos;
  St(ll a, string b, ll c) {
    val = a;
    str = b;
    pos = c;
  }
  bool operator<(const St &a) const {
    if (val != a.val)
      return val < a.val;
    return str != a.str ? str < a.str : pos < a.pos;
  }
  bool operator>(const St &a) const {
    if (val != a.val)
      return val > a.val;
    return str != a.str ? str > a.str : pos > a.pos;
  }
  // posが0なら頭から、1なら後ろから、-1ならアカン、2ならクリア
  void PRINT() { cout << val << ' ' << str << ' ' << pos << endl; }
};
vector<St> F, G;
set<string> SET;

bool kaibun(string S) {
  ll n = S.size();
  rep(i, n) {
    if (i >= n - 1 - i)
      return true;
    if (S[i] != S[n - 1 - i])
      return false;
  }
  return true;
}

St ABLE(St x, St y) {
  St akan(x.val + y.val, x.str + y.str, -1);
  if (x.pos == y.pos)
    return akan;
  if (x.pos == 1)
    return ABLE(y, x);
  St iketa(x.val + y.val, x.str + y.str, 2);
  ll X = x.str.size(), Y = y.str.size();
  if (X > Y) {
    rep(i, Y) if (x.str[i] != y.str[Y - 1 - i]) return akan;
    string kari = x.str.substr(Y);
    if (kaibun(kari))
      return iketa;
    St ret(x.val + y.val, kari, 0);
    return ret;
  }
  rep(i, X) if (x.str[i] != y.str[Y - 1 - i]) return akan;
  string kari = y.str.substr(0, Y - X);
  if (kaibun(kari))
    return iketa;
  St ret(x.val + y.val, y.str.substr(0, Y - X), 1);
  return ret;
}

ll Solve() {
  cin >> N;
  priority_queue<St, vector<St>, greater<St>> Q;
  rep(i, N) {
    string S;
    ll C;
    cin >> S >> C;
    St f(C, S, 1), g(C, S, 0), h(C, S, 2);
    if (!kaibun(S))
      Q.push(f);
    else
      Q.push(h);
    F.pb(f);
    G.pb(g);
  }
  sort(ALL(F));
  sort(ALL(G));
  while (!Q.empty()) {
    St f = Q.top();
    Q.pop();
    if (f.pos == 2)
      return f.val;
    else if (!SET.count(f.str)) {
      SET.insert(f.str);
      if (f.pos == 1) {
        rep(i, N) {
          St p = ABLE(f, G[i]);
          if (p.pos >= 0)
            Q.push(p);
        }
      } else {
        rep(i, N) {
          St p = ABLE(f, F[i]);
          if (p.pos >= 0)
            Q.push(p);
        }
      }
    }
  }
  return -1;
}
int main() { cout << Solve() << endl; }


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod

ll N;
struct St {
  ll val;
  string str;
  ll pos;
  St(ll a, string b, ll c) {
    val = a;
    str = b;
    pos = c;
  }
  bool operator<(const St &a) const {
    if (val != a.val)
      return val < a.val;
    return str != a.str ? str < a.str : pos < a.pos;
  }
  bool operator>(const St &a) const {
    if (val != a.val)
      return val > a.val;
    return str != a.str ? str > a.str : pos > a.pos;
  }
  // posが0なら頭から、1なら後ろから、-1ならアカン、2ならクリア
  void PRINT() { cout << val << ' ' << str << ' ' << pos << endl; }
};
vector<St> F, G;

bool kaibun(string S) {
  ll n = S.size();
  rep(i, n) {
    if (i >= n - 1 - i)
      return true;
    if (S[i] != S[n - 1 - i])
      return false;
  }
  return true;
}

St ABLE(St x, St y) {
  St akan(x.val + y.val, x.str + y.str, -1);
  if (x.pos == y.pos)
    return akan;
  if (x.pos == 1)
    return ABLE(y, x);
  St iketa(x.val + y.val, x.str + y.str, 2);
  ll X = x.str.size(), Y = y.str.size();
  if (X > Y) {
    rep(i, Y) if (x.str[i] != y.str[Y - 1 - i]) return akan;
    string kari = x.str.substr(Y);
    if (kaibun(kari))
      return iketa;
    St ret(x.val + y.val, kari, 0);
    return ret;
  }
  rep(i, X) if (x.str[i] != y.str[Y - 1 - i]) return akan;
  string kari = y.str.substr(0, Y - X);
  if (kaibun(kari))
    return iketa;
  St ret(x.val + y.val, y.str.substr(0, Y - X), 1);
  return ret;
}

ll Solve() {
  cin >> N;
  priority_queue<St, vector<St>, greater<St>> Q;
  rep(i, N) {
    string S;
    ll C;
    cin >> S >> C;
    St f(C, S, 1), g(C, S, 0);
    Q.push(f);
    F.pb(f);
    G.pb(g);
  }
  sort(ALL(F));
  sort(ALL(G));
  while (!Q.empty()) {
    St f = Q.top();
    Q.pop();
    if (f.pos == 2)
      return f.val;
    if (kaibun(f.str))
      return f.val;
    if (f.pos == 1) {
      rep(i, N) {
        St p = ABLE(f, G[i]);
        if (p.pos >= 0)
          Q.push(p);
      }
    } else {
      rep(i, N) {
        St p = ABLE(f, F[i]);
        if (p.pos >= 0)
          Q.push(p);
      }
    }
  }
  return -1;
}
int main() { cout << Solve() << endl; }