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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

typedef long long int int64;

const int X = (int)1e6 + 100;
char str[X];

const int ALP = 26;

int goTo[X][ALP];
int parc[X];
int term[X];

const int N = (int)2e5 + 100;
vector<int> pos[N];
int sz = 1;

void solve(int id) {
  scanf("%s", str);
  int n = strlen(str);
  reverse(str, str + n);

  int v = 0;
  for (int i = 0; i < n; i++) {
    int c = str[i] - 'a';
    if (goTo[v][c] == 0) {
      parc[sz] = c;
      goTo[v][c] = sz++;
    }
    pos[id].push_back(v);
    v = goTo[v][c];
  }
  pos[id].push_back(v);
  term[v] = id;
}

int answer;
int has[ALP];

int timer;

void addPair(int a, int b) {
  if (a > b)
    swap(a, b);
  if (a == -1 || a == b)
    return;
  answer++;
}

void getAns(int id) {
  // for (int x : pos[id])
  //     eprintf("%d ", x);
  // eprintf("\\n");
  memset(has, false, sizeof has);
  for (int i = (int)pos[id].size() - 1; i >= 0; i--) {
    int v = pos[id][i];
    //        addPair(id, term[v]);

    for (int j = 0; j < ALP; j++)
      if (has[j])
        addPair(id, term[goTo[v][j]]);

    has[parc[v]] = true;
  }
}

int main(int, char **) {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

  memset(term, -1, sizeof term);

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    solve(i);

  for (int i = 0; i < n; i++)
    getAns(i);

  printf("%d\\n", answer);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

typedef long long int int64;

const int X = (int)1e6 + 100;
char str[X];

const int ALP = 26;

int goTo[X][ALP];
int parc[X];
int term[X];

const int N = (int)2e5 + 100;
vector<int> pos[N];
int sz = 1;

void solve(int id) {
  scanf("%s", str);
  int n = strlen(str);
  reverse(str, str + n);

  int v = 0;
  for (int i = 0; i < n; i++) {
    int c = str[i] - 'a';
    if (goTo[v][c] == 0) {
      parc[sz] = c;
      goTo[v][c] = sz++;
    }
    pos[id].push_back(v);
    v = goTo[v][c];
  }
  pos[id].push_back(v);
  term[v] = id;
}

set<pair<int, int>> s;

int has[ALP];

void addPair(int a, int b) {
  if (a > b)
    swap(a, b);
  if (a == -1 || a == b)
    return;
  // eprintf("add %d %d\\n", a, b);
  s.insert(make_pair(a, b));
}

void getAns(int id) {
  // for (int x : pos[id])
  //     eprintf("%d ", x);
  // eprintf("\\n");
  memset(has, false, sizeof has);
  for (int i = (int)pos[id].size() - 1; i >= 0; i--) {
    int v = pos[id][i];
    addPair(id, term[v]);

    for (int j = 0; j < ALP; j++)
      if (has[j])
        addPair(id, term[goTo[v][j]]);

    has[parc[v]] = true;
  }
}

int main(int, char **) {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

  memset(term, -1, sizeof term);

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    solve(i);

  for (int i = 0; i < n; i++)
    getAns(i);

  printf("%d\\n", (int)s.size());

  return 0;
}