Source Code
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <unordered_map>
//#include"testlib.h"
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define sz(s)    (int)(s.size())
//#define PI acos(-1)
#define EPS 1e-8
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define mk(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("gcd.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}


void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
inline int get_num(deque<char>& q) {
	int ret = 0;
	for (auto& it : q)
		ret *= 10, ret += (it - '0');
	return ret;
}
int main() {
	file();
	fast();
	set<string> data_base;
	map <string, set<ii>> mp;
	auto add = [&](string str, int no) {
		mp[str].insert({ 1, (int)1e9 });
		deque<char> num;
		int cnt = 0;
		while (sz(str) > 1) {
			cnt++;
			num.push_front(str.back());
			str.pop_back();
			if (num.front() < '0' || num.front() > '9' || sz(num) > 7)
				break;
			if (num.front() == '0' || cnt == no)
				continue;
			int x = get_num(num);
			auto& st = mp[str];
			if (st.empty())
				st = set<ii>{ { 1, (int)1e9 } };
			auto it = st.upper_bound({ x, INT_MAX });
			it--;
			ii p = *it;
			st.erase(it);
			if (x - 1 >= p.first)
				st.insert({ p.first, x - 1 });
			if (x + 1 <= p.second)
				st.insert({ x + 1, p.second });
		}
	};
	int t; cin >> t;
	while (t--) {
		string str; cin >> str;
		if (data_base.find(str) == data_base.end()) {
			cout << "ok\n";
			data_base.insert(str);
			add(str, -1);
			continue;
		}
		auto& q = mp[str];
		ii it = *q.begin(); q.erase(q.begin());
		str += to_string(it.first);
		int no = sz(to_string(it.first));
		if (it.first != it.second)
			q.insert({ it.first + 1, it.second });
		cout << str << endl;
		data_base.insert(str);
		add(str, no);
	}
	return 0;
}
Copy
Registration System karemo
GNU G++17
190 ms
26.4 MB
Runtime Error