Source Code
#define ll long long
#define fr first
#define sc second
#define pb(x) push_back(x)
#define el '\n'
#define sz(container) ((ll) container.size())

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  ll n, i;
  string s, t;
  unordered_map<string, bool> mp;

  cin >> n;
  
  while (n--) {
    cin >> s;

    if (mp[s]) {
      for (i = 1;; ++i) {
        t = s + to_string(i);
        if (!mp[t]) {
          cout << t << el;
          mp[t] = 1;
          break;
        }
      }
    } else {
      cout << "ok\n";
    }
    
    for (i = sz(s) - 1; i > -1 && s[i] >= '0' && s[i] <= '9'; --i) {
      t = s.substr(0, i + 1);
      mp[s.substr(0, i + 1)] = 1;
    }
    
    t = s.substr(0, i + 1);
    mp[s.substr(0, i + 1)] = 1;
  }


  return 0;
}
Copy
Registration System Nedal
GNU G++17
3036 ms
992 KB
Time Limit Exceeded