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

/**
 * @brief atcoder
 * @author yao
 */
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>

typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;

#define N 262144
#define M 1048576
#define K (N * 2)
#define P 0xe6006a2cf4c46aedull

char buf[N + M];
char *ss[N];
int nss[N];
class HashTable {
public:
  uint data[K];
  uint &at(ulli k) {
    int pos = k % K;
    while (data[pos] && data[pos] != (uint)(k >> 32))
      pos = (pos + 1) % K;
    return data[pos];
  }
  bool get(ulli k) {
    k *= P;
    return at(k);
  }
  void set(ulli k) {
    k *= P;
    at(k) = k >> 32;
  }
} lu;
int cnt[128];

int main() {
  int n;
  scanf("%d", &n);
  char *p = buf;
  for (int i = 0; i < n; ++i) {
    scanf("%s", p), nss[i] = strlen(p);
    ss[i] = p, p += nss[i] + 1;
  }
  for (int i = 0; i < n; ++i)
    dbg_pri("poi 3: %s\\n", ss[i]);
  for (int i = 0; i < n; ++i) {
    ulli x = 0;
    for (int j = nss[i] - 1; j >= 0; --j)
      x *= P, x ^= ss[i][j];
    lu.set(x);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < nss[i]; ++j)
      ++cnt[ss[i][j]];
    ulli x = 0;
    for (int j = nss[i] - 1; j >= 0; --j) {
      x *= P;
      for (int k = 'a'; k <= 'z'; ++k)
        if (cnt[k])
          ans += lu.get(x ^ k);
      x ^= ss[i][j];
      --cnt[ss[i][j]];
    }
  }
  ans -= n;
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

/**
 * @brief atcoder
 * @author yao
 */
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>

typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;

#define N 262144
#define M 1048576
#define K (N * 2)
#define P 0xe6006a2cf4c46aedull

char buf[N + M];
char *ss[N];
int nss[N];
class HashTable {
public:
  std::pair<uint, int> data[K];
  int &operator[](ulli k) {
    k *= P;
    int pos = k % K;
    while (data[pos].ft && data[pos].ft != (uint)(k >> 32))
      pos = (pos + 1) % K;
    data[pos].ft = k >> 32;
    return data[pos].sd;
  }
} lu;
int cnt[128];

int main() {
  int n;
  scanf("%d", &n);
  char *p = buf;
  for (int i = 0; i < n; ++i) {
    scanf("%s", p), nss[i] = strlen(p);
    ss[i] = p, p += nss[i] + 1;
  }
  for (int i = 0; i < n; ++i)
    dbg_pri("poi 3: %s\\n", ss[i]);
  for (int i = 0; i < n; ++i) {
    ulli x = 0;
    for (int j = nss[i] - 1; j >= 0; --j)
      x *= P, x ^= ss[i][j];
    ++lu[x];
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < nss[i]; ++j)
      ++cnt[ss[i][j]];
    ulli x = 0;
    for (int j = nss[i] - 1; j >= 0; --j) {
      x *= P;
      for (int k = 'a'; k <= 'z'; ++k)
        if (cnt[k])
          ans += lu[x ^ k];
      x ^= ss[i][j];
      --cnt[ss[i][j]];
    }
  }
  ans -= n;
  printf("%d\\n", ans);
  return 0;
}