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

/*input
3
abcxyx
cyx
abc

*/
#include <bits/stdc++.h>
using namespace std;

const int N = 26; // changable

struct Trie {
  Trie *child[N];
  int endVal;
  Trie() {
    for (int i = 0; i < N; i++) {
      child[i] = NULL;
    }
    endVal = 0;
  }
  ~Trie() {
    for (int i = 0; i < N; i++) {
      delete child[i];
    }
  }
} *root = new Trie();

void add(string s) {
  Trie *cur = root;
  for (int i = 0; i < s.length(); i++) {
    int j = s[i] - 'a';
    if (cur->child[j] == NULL) {
      cur->child[j] = new Trie();
    }
    cur = cur->child[j];
  }
  cur->endVal++;
}

int search(string s) {
  Trie *cur = root;
  for (int i = 0; i < s.length(); i++) {
    int j = s[i] - 'a';
    if (cur->child[j] == NULL)
      return 0;
    cur = cur->child[j];
  }
  if (cur == NULL)
    return 0;
  else
    return cur->endVal;
}

long long res(string s) {
  Trie *cur = root;
  vector<int> cnt(N, 0);
  long long ans = 0;
  for (int i = 0; i < s.length(); i++) {
    cnt[s[i] - 'a']++;
  }
  for (int i = 0; i < s.length(); i++) {
    for (int k = 'a'; k <= 'z'; k++) {
      int jk = k - 'a';
      if (cnt[jk] > 0 && cur->child[jk] != NULL)
        ans += cur->child[jk]->endVal;
    }
    int j = s[i] - 'a';
    cnt[j]--;
    if (cur->child[j] == NULL)
      return ans;
    cur = cur->child[j];
  }
  return ans;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
#ifdef DEBUG
    cout << s[i] << endl;
#endif
    add(s[i]);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += res(s[i]);
  }
  cout << ans - n;
}


Error: TLE

Fixed submission:

/*input
3
abcxyx
cyx
abc

*/
#include <bits/stdc++.h>
using namespace std;

const int N = 26; // changable

struct Trie {
  Trie *child[N];
  int endVal;
  Trie() {
    for (int i = 0; i < N; i++) {
      child[i] = NULL;
    }
    endVal = 0;
  }
  ~Trie() {
    for (int i = 0; i < N; i++) {
      delete child[i];
    }
  }
} *root = new Trie();

void add(string s) {
  Trie *cur = root;
  for (int i = 0; i < s.length(); i++) {
    int j = s[i] - 'a';
    if (cur->child[j] == NULL) {
      cur->child[j] = new Trie();
    }
    cur = cur->child[j];
  }
  cur->endVal++;
}

int search(string s) {
  Trie *cur = root;
  for (int i = 0; i < s.length(); i++) {
    int j = s[i] - 'a';
    if (cur->child[j] == NULL)
      return 0;
    cur = cur->child[j];
  }
  if (cur == NULL)
    return 0;
  else
    return cur->endVal;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // int c;
  // while(c != 'b'){
  // 	c=getchar();
  // 	printf("%d\\n", c);
  // }
  int n;
  cin >> n;

  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
#ifdef DEBUG
    cout << s[i] << endl;
#endif
    add(s[i]);
  }
#ifdef DEBUG
  cout << "DEBUG" << endl;
#endif

  long long ans = 0;
  vector<int> cnt(N, 0);
  for (int i = 0; i < n; i++) {
    cnt.assign(N, 0);
    for (int j = 0; j < s[i].length(); j++) {
      cnt[s[i][j] - 'a']++;
    }
    for (int l = 0; l <= s[i].length(); l++) {
      string f = s[i].substr(0, l);
      if (l > 0)
        cnt[s[i][l - 1] - 'a']--;
#ifdef DEBUG
      cout << "Searching for: " << f << "\\n";
#endif // DEBUG
      for (char c = 'a'; c <= 'z'; c++) {
        if (cnt[c - 'a'])
          ans += search(f + c);
      }
    }
  }
  cout << ans - n;
}