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

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define F first
#define S second
#define pss pair<string, string>
#define pcc pair<char, char>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define vs vector<string>
#define vl vector<ll>
#define vs vector<string>
#define vll vector<pll>
#define vss vector<pss>
#define vcc vector<pcc>
#define MP make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define Very_fast                                                              \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
const int N = 2e5 + 5;
const int mod = 9998753;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
int TLEkiller[1000005][30];
map<pll, int> mp;
ll ans;
string s[N];
int n, cnt[30], qwq;
int main() {
  char c = getchar();
  while (c != ' ' && c != '\\n')
    n = n * 10 + c - '0', c = getchar();
  mp[MP(0, 0)] = ++qwq;
  for (int i = 0; i < n; i++) {
    char c = getchar();
    while (c != ' ' && c != '\\n')
      s[i] += c, c = getchar();
    for (int j = 0; j < s[i].size(); j++)
      cnt[s[i][j] - 'a']++;
    ll hash1 = 0, hash2 = 0, pw1 = 1, pw2 = 1;
    int num = 1;
    for (int j = (int)s[i].size() - 1; j >= 0; j--) {
      for (int k = 0; k < 26; k++)
        if (cnt[k])
          TLEkiller[num][k]++;
      hash1 = (ll)(s[i][j] - 'a' + 1) % mod * pw1 % mod + hash1;
      hash2 = (ll)(s[i][j] - 'a' + 1) % MOD * pw2 % MOD + hash2;
      hash1 %= mod;
      hash2 %= MOD;
      pw1 = pw1 * 113 % mod;
      pw2 = pw2 * 113 % MOD;
      if (!mp[MP(hash1, hash2)])
        mp[MP(hash1, hash2)] = ++qwq, num = qwq;
      else
        num = mp[MP(hash1, hash2)];
      cnt[s[i][j] - 'a']--;
    }
  }
  for (int i = 0; i < n; i++) {
    string S = s[i].substr(1, (int)s[i].size() - 1);
    reverse(S.begin(), S.end());
    ll hash1 = 0, hash2 = 0, pw1 = 1, pw2 = 1;
    for (int j = 0; j < S.size(); j++) {
      hash1 = (ll)(S[j] - 'a' + 1) % mod * pw1 % mod + hash1;
      hash2 = (ll)(S[j] - 'a' + 1) % MOD * pw2 % MOD + hash2;
      hash1 %= mod;
      hash2 %= MOD;
      pw1 = pw1 * 113 % mod;
      pw2 = pw2 * 113 % MOD;
    }
    ans += TLEkiller[mp[MP(hash1, hash2)]][s[i][0] - 'a'];
  }
  cout << ans - n << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define F first
#define pss pair<string, string>
#define pcc pair<char, char>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define vs vector<string>
#define vl vector<ll>
#define vs vector<string>
#define vll vector<pll>
#define vss vector<pss>
#define vcc vector<pcc>
#define MP make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define Very_fast                                                              \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int mp[30][1000005];
map<string, int> change;
ll ans;
string s[N];
int n, cnt[30], qwq;
int main() {
  char c = getchar();
  while (c != ' ' && c != '\\n')
    n = n * 10 + c - '0', c = getchar();
  change[""] = ++qwq;
  for (int i = 0; i < n; i++) {
    char c = getchar();
    while (c != ' ' && c != '\\n')
      s[i] += c, c = getchar();
    for (int j = 0; j < s[i].size(); j++)
      cnt[s[i][j] - 'a']++;
    int num = 1;
    string S = "";
    for (int j = (int)s[i].size() - 1; j >= 0; j--) {
      for (int k = 0; k < 26; k++)
        if (cnt[k])
          mp[k][num]++;
      S += s[i][j];
      if (!change[S])
        change[S] = ++qwq;
      num = change[S];
      cnt[s[i][j] - 'a']--;
    }
  }
  for (int i = 0; i < n; i++) {
    string S = s[i].substr(1, (int)s[i].size() - 1);
    reverse(S.begin(), S.end());
    ans += mp[s[i][0] - 'a'][change[S]];
  }
  cout << ans - n << "\\n";
  return 0;
}