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

// Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b, ll m) {
  ll res = 1;
  a %= m;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
  }
  return res;
}
ll modInverse(ll a, ll m) { return power(a, m - 2, m); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
int p1 = 37, p2 = 53;
const int m1 = mod;
const int m2 = mod + 2;
ll good[1000023];
map<pair<int, int>, bool> mark;
map<pair<int, int>, int> myany;
void compute_hash(string s) {
  ll mult = 1;
  ll ans1 = 0;
  ll ans2 = 0;
  for (int i = s.length() - 1; i >= 0; i--) {
    ans1 *= p1;
    ans1 += s[i];
    ans1 %= m1;
    ans2 *= p2;
    ans2 += s[i];
    ans2 %= m2;
    myany[mp(ans1, ans2)]++;
  }
  mark[mp(ans1, ans2)] = 1;
}
// ll compute_hash(string s,ll p,ll m)
// {
//     ll mult=1;
//     ll ans=0;
//     for(int i=0;i<s.length();i++)
//     {
//         ans+=(s[i]*mult);
//         ans%=m;
//         mult*=p;
//         mult%=m;
//     }
//     return ans;
// }
void solve() {
  // cout<<compute_hash("yx",p1,m1)<<' '<<compute_hash("yx",p2,m2)<<'\\n';
  ll n;
  cin >> n;
  vs v;
  myany[mp(0, 0)] = 1;
  rep(i, n) {
    string s;
    cin >> s;
    v.pb(s);
    compute_hash(s);
    good[s.length()] = 1;
  }
  // cout<<myany[mp(4561,6481)]<<'\\n';
  ll ans = 0;
  for (auto s : v) {
    ll len = s.length();
    int cnt[28] = {0};
    for (int i = 0; i < len; i++) {
      cnt[s[i] - 'a']++;
    }
    ll suff1 = 0, suff2 = 0;
    for (int req = 1; req < len; req++) {
      if (req == 1) {
        for (char ch = 'a'; ch <= 'z'; ch++) {
          if (cnt[ch - 'a'] > 0) {
            ll now = ch;
            ans += mark[mp(now, now)];
          }
        }
        continue;
      }
      cnt[s[len - req + 1] - 'a']--;
      suff1 *= p1;
      suff1 += (s[len - req + 1] * p1);
      suff1 %= m1;
      suff2 *= p2;
      suff2 += (s[len - req + 1] * p2);
      suff2 %= m2;
      ll x = suff1 * modInverse(p1, m1);
      x %= m1;
      ll y = suff2 * modInverse(p2, m2);
      y %= m2;
      // cout<<s<<' '<<req<<' '<<x<<' '<<y<<' '<<suff1/p1<<' '<<suff2/p2<<'\\n';
      if (myany[mp(x, y)] <= 1)
        break;
      if (good[req] == 0)
        continue;
      for (char ch = 'a'; ch <= 'z'; ch++) {
        if (cnt[ch - 'a'] > 0) {
          ll now1 = (suff1 + ch) % m1;
          ll now2 = (suff2 + ch) % m2;
          ans += mark[mp(now1, now2)];
        }
      }
    }
  }
  cout << ans << '\\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin>>T;
  int t = 0;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}


Error: TLE

Fixed submission:

// Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll modInverse(ll a) { return power(a, mod - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
int p1 = 37, p2 = 53;
int m1 = 998244353;
int m2 = 509;
ll good[1000023];
ll compute_hash(string s, ll p, ll m) {
  ll mult = 1;
  ll ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans += (s[i] * mult);
    ans %= m;
    mult *= p;
    mult %= m;
  }
  return ans;
}
void solve() {
  ll n;
  cin >> n;
  vs v;
  map<pair<ll, ll>, bool> mark;
  vector<pair<ll, ll>> temp;
  rep(i, n) {
    string s;
    cin >> s;
    v.pb(s);
    ll a = compute_hash(s, p1, m1);
    ll b = compute_hash(s, p2, m2);
    mark[mp(a, b)] = 1;
    good[s.length()] = 1;
  }
  ll ans = 0;
  for (auto s : v) {
    ll len = s.length();
    int cnt[28] = {0};
    for (int i = 0; i < len; i++) {
      cnt[s[i] - 'a']++;
    }
    ll suff1 = 0, suff2 = 0;
    for (int req = 1; req < len; req++) {
      if (req == 1) {
        for (char ch = 'a'; ch <= 'z'; ch++) {
          if (cnt[ch - 'a'] > 0) {
            ll now = ch;
            ans += mark[mp(now, now)];
          }
        }
        continue;
      }
      cnt[s[len - req + 1] - 'a']--;
      suff1 *= p1;
      suff1 += (s[len - req + 1] * p1);
      suff1 %= m1;
      suff2 *= p2;
      suff2 += (s[len - req + 1] * p2);
      suff2 %= m2;
      if (good[req] == 0)
        continue;
      for (char ch = 'a'; ch <= 'z'; ch++) {
        if (cnt[ch - 'a'] > 0) {
          ll now1 = (suff1 + ch) % m1;
          ll now2 = (suff2 + ch) % m2;
          ans += mark[mp(now1, now2)];
        }
      }
    }
  }
  cout << ans << '\\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin>>T;
  int t = 0;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}