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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define rep(i, m, n) for (int(i) = (int)(m); (i) < (int)(n); (i)++)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; (i) >= (int)(m); (i)--)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define FOR(i, c)                                                              \\
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)

#define ull unsigned long long
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\\n'

using ll = long long;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;

typedef vector<ll> Array;
typedef vector<Array> Matrix;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

// constexpr long long MOD = (ll) 1e9+7;
constexpr long long MOD = 998244353;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct Edge {
  int to, cap, rev;
  Edge(int _to, int _cap, int _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, int from, int to, int cap, bool revFlag, int revCap) {
  G[from].push_back(Edge(to, cap, G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, G[from].size() - 1));
}

void solve() {
  ll n;
  cin >> n;
  vvec<string> s(2, vec<string>(n));
  vec<ll> c(n);
  REP(i, n) {
    cin >> s[0][i] >> c[i];
    s[1][i] = s[0][i];
    reverse(all(s[1][i]));
  }
  using T = tuple<ll, bool, string>;
  priority_queue<T, vec<T>, greater<T>> que;

  vec<map<string, ll>> mp(2);
  auto push = [&](ll cost, bool flag, string t) {
    if (!mp[flag].count(t) or mp[flag][t] > cost) {
      mp[flag][t] = cost;
      que.push({cost, flag, t});
    }
  };
  REP(i, n) { push(c[i], 1, s[0][i]); }
  while (que.size()) {
    auto [cost, flag, t] = que.top();
    que.pop();

    ll m = t.size();
    bool pal = true;
    REP(i, m / 2) {
      if (t[i] != t[m - 1 - i])
        pal = false;
    }
    if (pal) {
      cout << cost << en;
      return;
    }

    REP(i, n) {
      ll mm = s[flag][i].size();
      ll k = min(mm, m);
      bool same = true;
      REP(j, k) {
        if (s[flag][i][j] != t[j])
          same = false;
      }
      if (same) {
        if (m == mm) {
          push(cost + c[i], 0, "");
        } else if (m < mm) {
          push(cost + c[i], !flag, s[flag][i].substr(k, mm - m));
        } else {
          push(cost + c[i], flag, t.substr(k, m - mm));
        }
      }
    }
  }

  cout << -1 << en;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  solve();
  // int t; cin >> t; REP(i, t) solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define rep(i, m, n) for (int(i) = (int)(m); (i) < (int)(n); (i)++)
#define rep2(i, m, n) for (int(i) = (int)(n)-1; (i) >= (int)(m); (i)--)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define FOR(i, c)                                                              \\
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)

#define ull unsigned long long
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\\n'

using ll = long long;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;

typedef vector<ll> Array;
typedef vector<Array> Matrix;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

// constexpr long long MOD = (ll) 1e9+7;
constexpr long long MOD = 998244353;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct Edge {
  int to, cap, rev;
  Edge(int _to, int _cap, int _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, int from, int to, int cap, bool revFlag, int revCap) {
  G[from].push_back(Edge(to, cap, G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, G[from].size() - 1));
}

void solve() {
  ll n;
  cin >> n;
  vvec<string> s(2, vec<string>(n));
  vec<ll> c(n);
  REP(i, n) {
    cin >> s[0][i] >> c[i];
    s[1][i] = s[0][i];
    reverse(all(s[1][i]));
  }
  using T = tuple<ll, bool, string>;
  priority_queue<T, vec<T>, greater<T>> que;
  REP(i, n) { que.push({c[i], 1, s[0][i]}); }
  while (que.size()) {
    auto [cost, flag, t] = que.top();
    que.pop();

    ll m = t.size();
    bool pal = true;
    REP(i, m / 2) {
      if (t[i] != t[m - 1 - i])
        pal = false;
    }
    if (pal) {
      cout << cost << en;
      return;
    }

    REP(i, n) {
      ll mm = s[flag][i].size();
      ll k = min(mm, m);
      bool same = true;
      REP(j, k) {
        if (s[flag][i][j] != t[j])
          same = false;
      }
      if (same) {
        if (m == mm) {
          que.push({cost + c[i], 0, ""});
        } else if (m < mm) {
          que.push({cost + c[i], !flag, s[flag][i].substr(k, mm - m)});
        } else {
          que.push({cost + c[i], flag, t.substr(k, m - mm)});
        }
      }
    }
  }

  cout << -1 << en;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  solve();
  // int t; cin >> t; REP(i, t) solve();

  return 0;
}