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

#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, e, i, j, ii, jj, zx, xc, mod[2],
    pp = 31, xar[1000009][2], hsh[1000009][2], fx[39], kl[2], pas;
string ss[200009], s;
char cha[1000009];
unordered_map<long long, vector<long long>> m;
vector<long long> v, vv;
long long mk(long long q, long long w) { return q * 1000000021LL + w; }
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  mod[0] = 1000000007LL;
  mod[1] = 1000000009LL;
  xar[0][0] = xar[0][1] = 1;
  for (i = 1; i <= 1000003; i++) {
    for (j = 0; j <= 1; j++) {
      xar[i][j] = xar[i - 1][j] * pp;
      xar[i][j] %= mod[j];
    }
  }
  // cin>>a;
  scanf("%I64d\\n", &a);
  for (i = 1; i <= a; i++) {
    scanf("%s\\n", &cha);
    for (j = 0;; j++) {
      if (cha[j] < 'a' || cha[j] > 'z')
        break;
      ss[i].push_back(cha[j]);
    }
    // cout<<ss[i]<<endl;
    // cin>>ss[i];
  }
  for (i = 1; i <= a; i++) {
    s = ss[i];
    b = s.size();
    // cout<<s<<endl;
    s.insert(0, "0");
    hsh[b + 1][0] = hsh[b + 1][1] = 0;
    for (j = b; j >= 1; j--) {
      for (jj = 0; jj <= 1; jj++) {
        hsh[j][jj] = hsh[j + 1][jj] + xar[b - j][jj] * (s[j] - 'a' + 1);
        hsh[j][jj] %= mod[jj];
      }
    }
    v = m[mk(hsh[2][0], hsh[2][1])];
    if (v.size() == 0) {
      v.resize(28);
      v[s[1] - 'a']++;
    } else {
      v[s[1] - 'a']++;
    }
    m[mk(hsh[2][0], hsh[2][1])] = v;
    /*        for(ii=0; ii<=5; ii++) cout<<v[ii]<<" ";
            cout<<endl;*/
    // cout<<mk(hsh[1][0],hsh[1][1])<<endl;
  }
  // cout<<endl<<endl;
  for (i = 1; i <= a / 2; i++)
    swap(ss[i], ss[a - i + 1]);
  for (i = 1; i <= a; i++) {
    s = ss[i];
    b = s.size();
    // cout<<s<<endl;
    s.insert(0, "0");
    hsh[b + 1][0] = hsh[b + 1][1] = 0;
    for (ii = 0; ii <= 27; ii++)
      fx[ii] = 0;
    for (j = b; j >= 1; j--) {
      for (jj = 0; jj <= 1; jj++) {
        hsh[j][jj] = hsh[j + 1][jj] + xar[b - j][jj] * (s[j] - 'a' + 1);
        hsh[j][jj] %= mod[jj];
      }
    }
    for (j = 2; j <= b + 1; j++) {
      fx[s[j - 1] - 'a'] = 1;
      v = m[mk(hsh[j][0], hsh[j][1])];
      if (v.size() == 0)
        continue;
      /*cout<<i<<" "<<j<<endl;
      for(ii=0; ii<=5; ii++) cout<<v[ii]<<" ";
      cout<<endl;*/
      for (ii = 0; ii < 26; ii++) {
        if (v[ii] != 0 && fx[ii] != 0)
          pas += v[ii];
      }
    }
    // cout<<mk(hsh[1][0],hsh[1][1])<<endl;
  }
  // printf("%I64d",pas);
  cout << pas - a << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, e, i, j, ii, jj, zx, xc, mod[2],
    pp = 31, xar[1000009][2], hsh[1000009][2], fx[39], kl[2], pas;
string ss[200009], s;
char cha[1000009];
unordered_map<long long, long long> m;
long long mk(long long q, long long w) { return q * 1000000021LL + w; }
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  mod[0] = 1000000007LL;
  mod[1] = 1000000009LL;
  xar[0][0] = xar[0][1] = 1;
  for (i = 1; i <= 1000003; i++) {
    for (j = 0; j <= 1; j++) {
      xar[i][j] = xar[i - 1][j] * pp;
      xar[i][j] %= mod[j];
    }
  }
  // cin>>a;
  scanf("%I64d\\n", &a);
  for (i = 1; i <= a; i++) {
    scanf("%s\\n", &cha);
    for (j = 0;; j++) {
      if (cha[j] < 'a' || cha[j] > 'z')
        break;
      ss[i].push_back(cha[j]);
    }
    // cout<<ss[i]<<endl;
    // cin>>ss[i];
  }
  for (i = 1; i <= a; i++) {
    s = ss[i];
    b = s.size();
    // cout<<s<<endl;
    s.insert(0, "0");
    hsh[b + 1][0] = hsh[b + 1][1] = 0;
    for (j = b; j >= 1; j--) {
      for (jj = 0; jj <= 1; jj++) {
        hsh[j][jj] = hsh[j + 1][jj] + xar[b - j][jj] * (s[j] - 'a' + 1);
        hsh[j][jj] %= mod[jj];
      }
    }
    m[mk(hsh[1][0], hsh[1][1])]++;
    // cout<<mk(hsh[1][0],hsh[1][1])<<endl;
  }
  for (i = 1; i <= a / 2; i++)
    swap(ss[i], ss[a - i + 1]);
  for (i = 1; i <= a; i++) {
    s = ss[i];
    b = s.size();
    // cout<<s<<endl;
    s.insert(0, "0");
    hsh[b + 1][0] = hsh[b + 1][1] = 0;
    for (ii = 0; ii <= 27; ii++)
      fx[ii] = 0;
    for (j = b; j >= 1; j--) {
      for (jj = 0; jj <= 1; jj++) {
        hsh[j][jj] = hsh[j + 1][jj] + xar[b - j][jj] * (s[j] - 'a' + 1);
        hsh[j][jj] %= mod[jj];
      }
    }
    for (j = 2; j <= b + 1; j++) {
      fx[s[j - 1] - 'a'] = 1;
      for (ii = 0; ii < 26; ii++) {
        if (fx[ii] != 0) {
          for (jj = 0; jj <= 1; jj++) {
            kl[jj] = hsh[j][jj] + xar[b - j + 1][jj] * (ii + 1);
            kl[jj] %= mod[jj];
          }
          pas += m[mk(kl[0], kl[1])];
          // cout<<mk(kl[0],kl[1])<<endl;
        }
      }
    }
    // cout<<mk(hsh[1][0],hsh[1][1])<<endl;
  }
  // printf("%I64d",pas);
  cout << pas - a << endl;
  return 0;
}