We need to solve a problem: given n usernames, process each registration request. If the name hasn't been seen before, output "OK" and insert it. If it has been seen, we need to find the smallest positive integer i such that name + str(i) (like name1, name2, ...) is not yet in the database; then output that namei, and also insert that namei into the database.
Constraints: n up to 1e5, names length <= 32, all lowercase letters.
We need to efficiently find the next available suffix for a given name. Straightforward: maintain a map from base name to the next available index (counter) maybe. But careful: the numbers we assign are not necessarily sequential for all cases? Actually the process: If name is already in db, we need to find the smallest i >= 1 such that namei not used. Since we are inserting these namei into db, we can keep track of the largest i used for that base name? But we might also have gaps: Suppose base name "a" exists and we have "a1" inserted; then later we get "a" again. The smallest unused i could be 1? But "a1" is already taken, so i=1 is taken, need to check i=2. However if we then later get "a" again, maybe we need to check i=1? Actually i=1 is still taken, i=2 also taken? Let's simulate: First "a": OK -> insert "a". Second "a": check name1, not exists -> output "a1" and insert "a1". Third "a": check name1 exists, name2 not exists -> output "a2", insert "a2". Fourth "a": check name1 exists, name2 exists, name3 not -> output "a3". So it's sequential. But consider if we also have direct registration of "a2" before it appears naturally? Actually users only send base names. They never send names with numbers. However the system inserts the prompted name with number. So the only names inserted are base names (when first encountered) and namei for some base name and i. So for a given base name, the set of names inserted includes the base name and some suffixes. These suffixes are generated sequentially from the smallest available each time. Thus there will be no gaps unless we have a scenario where base name is "a", we get "a" -> insert "a". Then later we might have "a1" as a request? Wait the input requests are only base names? According to problem: "Each time a new user wants to register, he sends to the system a request with his name." The name is just a string; there is no restriction that it doesn't contain digits? The input specification says "Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters." So indeed the requests consist only of lowercase letters, no digits. So the only names with numbers are those generated by system, not requested. So no direct registration of namei.
Thus for each base name, the only generated suffixes are sequential from 1 upward, and there won't be gaps. However there is a nuance: Suppose we have base name "a". Insert "a". Then later we get "a". We generate "a1". Then later we get "a". We generate "a2". That's sequential. So we can simply keep a counter for each base name indicating how many times it has been seen (including the original registration? Actually careful: The base name itself is inserted on first occurrence. After that, each subsequent occurrence triggers generation of namei, and we insert that namei. The counter to use for next i: after first occurrence (count=1) we have base name registered. On second occurrence, we want i=1; on third, i=2; etc. So if we let count be the number of times we have seen this base name so far (including the current request?), we can compute: if count == 1 (first time), output "OK" and register base name; else output base name + (count-1) and register that. That seems to work because we never skip any numbers. But we must also consider that the generated namei becomes part of the database, so later requests for the base name must check if namei exists. But since we are generating them in order, and they are unique, count-1 will be the smallest available i.
However, there is a subtlety: what if a generated name like "ab1" conflicts with a different base name? For example base name "ab" and base name "ab1"? But note base names are only lowercase letters. So "ab1" cannot be a request (since requests are only letters). So the only way "ab1" appears is as a generated name. Could it conflict with a base name like "ab"? That's different because "ab1" contains a digit, so it's different from "ab". So no conflict. But wait: The database stores strings; when we check if a name exists, we must consider both base names and generated names. So for a base name "a", we might generate "a1". Could there be a base name "a1"? No, because base names are only letters, so "a1" would contain a digit and not be requested. So no conflict. However, what if a base name is something like "abc" and later we generate "abc1". That's distinct. So the only possible conflict is when two different base names produce the same generated string? For example base name "ab" with i=12 yields "ab12", and base name "ab1" with i=2 yields "ab12"? But base name "ab1" is not possible because it contains digit. So no. However there could be a base name "a" and another base name "a" are same base, that's fine. So basically each base name's generated names are disjoint from other base names? Not necessarily; consider base name "a" and base name "a1"? But "a1" can't be a base name because it contains digit. So safe.
But wait: What about base name that itself could be a suffix of another? For example base name "ab" and base name "abab"? They are both letters. The generated name for "ab" could be "ab1", "ab2", etc. Could "ab1" equal "ab" + "1" and be same as base name "ab1"? No, base names are only letters, so no. So no collisions.
Thus the straightforward count method should work: For each base name, keep a counter indicating how many times it has been encountered. For the first encounter, output "OK" and set counter=1. For subsequent encounters, output name + (counter) (since counter currently holds the number of previous encounters, and the next available i is counter) and then increment counter (or set counter++ after insertion). But we also need to insert the new name into the database. Actually careful: When we output name + str(counter), we need to also insert that name into the database. And increment the counter for that base name so that next time we use counter+1. But also we must be careful: The base name itself remains in the database, and we should not overwrite its presence. So we can just maintain a map from string to something? Actually we don't need to store the whole database if we only care about counts? But we do need to know if a name (the base name) was already seen. That can be stored in a map of base names to counters. Additionally, when we generate a name with a number, we need to ensure that it doesn't conflict with future base names? But base names are only letters, so it's fine. However, the generated name might later be encountered as a base name? No, because base names are only letters, they can't contain digits. So they won't be the same as generated names. So we don't need to store generated names in the map for checking base name conflicts? But we do need to avoid reusing the same generated name for the same base name? Since we are generating sequentially and never skipping, and we will never generate the same number twice for the same base name because we increment the counter each time. So it's safe without storing generated names? However, what about a scenario: base name "a" appears first: insert "a". Then later we get a request "a". We generate "a1" and insert "a1". Then later we get a request "a". We generate "a2". So far so good. But what if we later get a request "a1"? That can't happen because it contains a digit. So no. So we don't need to store generated names at all? But is there any possibility that a generated name could be the same as a base name from a different base? As argued, no, because digits not allowed in base names. So we can just keep a map of base names to counters. But wait: What about a base name like "a0"? Not allowed. So it's fine. However, there is a nuance: The problem statement says "If such a name does not exist in the system database, it is inserted into the database... The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, ...), among these numbers the least i is found so that namei does not yet exist in the database." So we must actually check the database for existence of namei. In our sequential generation, we guarantee that namei will not exist if we increment a counter, because we never inserted that specific suffix before. But could it exist because of some other reason? For instance, if we have base name "a", we inserted "a" and then later we inserted "a1". Suppose later we get another request "a". We will attempt to find smallest i such that "a" + i not in database. According to our counter method, we'd suggest i=2, but what if "a2" already exists? It could exist if some other base name generated it? Only if there is base name "a2"? Not allowed. Or if we had earlier a different base name that generated "a2"? Could that happen? For base name "a2"? Not allowed. Or base name "a" with different generation? But we haven't generated "a2" yet if counter was 2? Actually after two requests after first, we generated "a1". So only "a" and "a1" exist. So i=2 is available. So no conflict. So sequential counter works.
But wait: There is an important nuance: The base name itself might be something like "abacaba". After the first occurrence we output OK and insert "abacaba". The second occurrence we generate "abacaba1". But what if the first occurrence already had a number from previous operations? Actually it's just base name. So fine.
Thus the solution is simply: use an unordered_map (or map) from string to int, storing how many times that base name has been used (i.e., how many registrations have been processed for that base name). For each request s:
- Let cnt = map[s] (default 0).
- If cnt == 0: output "OK", and set map[s] = 1.
- Else: output s + to_string(cnt), and set map[s] = cnt + 1.
That's it.
But is there any scenario where this fails due to the fact that the generated name might already exist because some other base name could have a mapping that produces that string? For example base name "a" and base name "a1"? "a1" is not a valid base name. So not possible.
But wait: base names are only lowercase letters. However generated names are base name + number. So they have digits at the end. Could there be a base name that is exactly "a" and another base name that is "a"+"1"? No, because the latter contains digit '1'. So no. So indeed the set of base names (only letters) is disjoint from set of generated names (letters+digits). So no collisions across base names.
Thus the solution is straightforward.
But is there any scenario where two different base names could produce same generated string? For base s1 and s2 with numbers i1 and i2 such that s1 + str(i1) == s2 + str(i2). Since s1 and s2 are both lowercase strings without digits, and we append digits, the only way they could be equal is if s1 is a prefix of s2 (or vice versa) and the rest is digits. For example s1 = "abc", s2 = "abc1". Then "abc" + "12" = "abc12" and "abc1" + "2" = "abc12"? But s2 = "abc1" contains a digit, so it's not a valid base name. So s2 cannot have digits. Therefore s1 and s2 are both pure letters. If we have s1 != s2, could s1 + digits == s2 + digits? For them to be equal, the strings must match exactly. If s1 and s2 are different pure letter strings, then the prefixes differ, so the concatenated strings differ. Unless the digit part changes but digits are appended after the whole base name. So no.
Thus the method is safe.
But there's a nuance: The generated names become part of the database. Could they affect the registration of base names? No, because when checking if a base name exists, we only look for the base name string. The base name string doesn't contain digits. So the existence of "abc1" doesn't affect check for "abc". But it might affect the check for future generated names for the same base name, but we are using a counter to keep track, which is okay.
Thus the solution is trivial. However, wait, let's test with sample:
Input:
4
abacaba
acaba
abacaba
acab
Our map:
abacaba: first: cnt=0 -> output OK, map[abacaba]=1
acaba: cnt=0 -> OK, map[acaba]=1
abacaba: cnt=1 -> output abacaba1, map[abacaba]=2
acab: cnt=0? Actually "acab" is not same as "acaba", so cnt=0 -> OK, map[acab]=1
Output:
OK
OK
abacaba1
OK -> matches sample.
Second sample:
first: cnt=0 -> OK, map[first]=1
first: cnt=1 -> first1, map[first]=2
second: cnt=0 -> OK, map[second]=1
second: cnt=1 -> second1, map[second]=2
third: cnt=0 -> OK, map[third]=1
third: cnt=1 -> third1, map[third]=2
Output matches.
So solution accepted.
But wait: Let's think about a more complicated scenario: Suppose we have base name "a". Then we have "a" -> OK. Then we have "a" -> "a1". Then we have "a1"? But can't. Then we have "a" -> "a2". So far so good.
But what about name collisions across different base names: Suppose base name "abc" and "abc1"? Not allowed.
Thus solution stands.
But let's double-check constraints: n up to 1e5, names length <= 32. Map operations O(1) average. So fine.
Edge cases: When counter becomes large, we need to append numbers that could be large; but string length <= 32 originally, plus digits. But the problem doesn't limit output length? Possibly could exceed 32? Actually input names are at most 32 characters. The generated names could be longer because we append numbers. But the problem statement says "Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters." It doesn't restrict output length. But the prompt name must be inserted into database. Since we could have many numbers, length could increase. But that's fine.
One more nuance: Since we only store counter per base name, and we never store the generated names, could there be a scenario where a generated name from one base name might conflict with the counter of another base name? Suppose base name "ab" and we have many registrations of "ab", we generate "ab1", "ab2", ... "ab100". Now what about base name "ab100"? Not allowed. So no.
But what about base name "a" and base name "a0"? Not allowed. So fine.
Thus the solution is as simple as described.
However, many solutions on Codeforces (this is CF problem 4A? Actually it's "Registration System" problem 4C? Yes, Codeforces 4C "Registration system") use exactly this approach. I recall the problem is known. So it's fine.
But wait, check problem statement: "A new e-mail service 'Berlandesk' ..." This is indeed Codeforces 4C: Registration system. I think that problem allowed any strings, not only lowercase letters? Actually in CF 4C, the strings could be any lowercase letters? I think it's lowercase letters only? Let's recall: In CF 4C problem, the input strings consist of lowercase Latin letters only, up to 32 characters. So yes.
Thus solution: Use map<string, int> to count occurrences. For each input name s, if count[s] == 0, output "OK", count[s] = 1. Else output s + to_string(count[s]) and count[s]++.
Time O(n) average.
But careful: For the first occurrence, we set count=1 after output. For subsequent, we output s+count and then increment. Because count currently holds the number of times we've seen it before (including base and generated). Actually if count is the number of times we have already encountered that base name (including the first), then on second occurrence count=1, we want output s+"1". So we output s+count and then count++. So that's right.
Implementation details: Use std::unordered_map or std::map. Since n is only 1e5, both fine. But unordered_map may be faster.
Edge case: Name could be up to 32 characters, but we might generate many numbers, but that's fine.
Thus answer.
But wait: The problem also requires to insert the prompt into the database. Our approach does not explicitly store the prompt; but we store only the counter. However, the existence of the prompt matters for checking future names only if that prompt could be used as a base name? Since base names can't contain digits, they will never equal the prompt. However, future prompts for the same base name are generated sequentially, we ensure they don't conflict. But what if a different base name is such that its base name equals a previously generated prompt? That can't happen because base names are only letters, prompts contain digits. So no need to store prompts. But there is a twist: What if a generated name like "abc123" could later be requested as a base name? No, because base names are only lowercase letters, so digits not allowed. So safe.
Thus we don't need to store prompts.
However, if we wanted to be absolutely correct by the problem statement, we might need to consider that the database includes both base names and generated names. But since they are disjoint sets, we can ignore.
But there is a potential scenario: The problem statement says: "If such a name does not exist in the system database, it is inserted into the database... The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, ...), among these numbers the least i is found so that namei does not yet exist in the database." It does not guarantee that base names are only letters? It says: "Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters." So requests are only letters. The generated names are namei, which are base name concatenated with number, which will have digits. So indeed they are disjoint. So fine.
Thus solution is trivial.
But let's consider a trick: What if we have a base name that is same as a generated name from another base name? Not possible.
So answer.
Given the user asked: "You're suggested to implement the prototype of site registration system." It's a straightforward problem.
Thus we'll provide a solution in C++.
Implementation:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
unordered_map<string, int> db;
string s;
for (int i = 0; i < n; ++i) {
cin >> s;
auto it = db.find(s);
if (it == db.end()) {
cout << "OK\n";
db[s] = 1;
} else {
int cnt = it->second;
cout << s << cnt << "\n";
db[s] = cnt + 1;
}
}
return 0;
}
That's it.
But wait: Actually we might need to also insert the generated name into the database? The problem says: "and also inserts the prompt into the database." So we should also insert the generated name to avoid future collisions. But as argued, it's not needed because they won't conflict. However, strictly speaking, if we don't insert it, and later a request for the same base name comes again, we would generate the next number, but we might generate the same number again? No, because we are using a counter, we wouldn't generate the same number because we increment. But could there be a scenario where the generated name might be generated again? Only if the counter resets, but it doesn't. So it's safe.
However, consider a scenario where a generated name could be used as a base name? No.
But what about the case where a base name equals some other base name's generated name? Not possible because generated names contain digits. So fine.
Thus we don't need to insert generated names into the map. However, some solutions do insert them to be safe but it's unnecessary. If we wanted to insert, we would have to store the generated name in the map as well. But then the map key space would expand to include generated names, which could potentially cause collisions with future base names? Still no, because base names are letters only. But if we insert generated names, they become part of the database, and we would need to check if a request for a base name matches a generated name? No, because requests are only letters, so they cannot match a generated name (since generated name has digits). So it's fine. However, it might affect the counter method because then the generated names would be stored as separate entries. But they have a different key, so they won't interfere with the counter for the base name. So we could also just store the generated names as separate entries with something? Actually we could, but not needed.
Given the problem statement explicitly says to insert the prompt into the database, we could do it to be correct by the literal description. However, does it affect the algorithm if we do? If we insert the prompt, then later when we process another request for the same base name, we will still use the counter to generate the next number. But the prompt we inserted will be stored as a key; however, we never check that key for the base name's existence because it's a different string. So it's fine. But what if we inserted the prompt and then later a request for the same base name might cause us to check existence of "namei" by scanning i? In our algorithm we don't actually check existence; we just rely on the counter. But if we inserted the prompt, it's consistent: we are using the smallest i that doesn't exist, and we are generating i equal to the counter. Since we inserted previous prompts, they exist, and counter ensures we don't repeat them. So it's consistent.
So we could also update the map for the prompt? But that would require using a separate map or storing something? Actually if we want to store the prompt to reflect that it exists, we could just store it as a key in the same map, but then it would have its own counter which we might need to handle if a request for that prompt came? But no, requests for that prompt cannot happen because prompts contain digits. So it's safe to store them with a dummy value (maybe 0 or 1). However, if we store it with value 0, then later if by some chance we need to check existence, it will be present. But we don't need to check existence for prompts, only for base names. But it could affect the base name's counter? No, because we only update the base name's counter. If we accidentally insert the prompt with key prompt and some value, that's separate. So we could do something like: after outputting s+to_string(cnt), we set db[s+to_string(cnt)] = 0 (or 1). But we don't need to because prompts won't be requested.
Given typical solutions on Codeforces, they do not insert prompts; they just use a map from base name to count. I've seen many accepted solutions using exactly that.
Thus answer is as described.
But to be thorough, let's think about a possible pitfall: What if the base name is something like "a", and we have many requests, eventually the counter becomes, say, 1000. We output "a1000". That string is 5 characters. That's fine. There's no length limit for output. So okay.
Now, confirm with constraints: n up to 100k, so output size is manageable.
Thus answer.
But the user might want an explanation, not just code. The problem statement says "You're suggested to implement the prototype". So we need to provide code and explanation.
Thus we will respond with a solution.
Now, I'll write a comprehensive answer, including explanation of algorithm, complexity analysis, and code in C++ (or maybe Python). Since the user didn't specify language, but typical on Codeforces, C++ is common. We can provide both? But usually one language is enough.
Given the assistant's role: we should output the answer as a final message, with solution and explanation.
Thus final answer.
But we should be careful: The user said "A new e-mail service ..." This is indeed Codeforces problem 4C. Many solutions exist. So we can provide.
Thus I will produce the answer.
I'll include:
-
Approach: Use a hashmap to keep count of occurrences of each base name. For each input name s, if it's the first time, output OK and set count[s]=1. Otherwise, output s + count[s] and increment count[s].
-
Why this works: Because requests are only lowercase letters, generated names contain digits, so no collisions. The counter ensures we use smallest unused integer.
-
Complexity: O(n) average time, O(n) memory.
-
Code.
Thus final. We can solve this problem using a simple map (or unordered_map) to keep track of how many times each name has been encountered. Since all requests consist only of lowercase Latin letters, and the generated names contain digits, there is no risk of collision between a base name and a generated name. For a given name, the first time it appears we output "OK" and remember that we have seen it once. On each subsequent appearance we output the name followed by the current count (which is the smallest unused integer suffix) and then increase the count for that name. This guarantees that we always produce the smallest available suffix without needing to explicitly store the generated names.
Complexity:
- Time: O(n) on average (each map operation is O(1)).
- Space: O(n) for storing the counts.
Here is the implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
unordered_map<string, int> cnt;
string s;
while (n--) {
cin >> s;
int &c = cnt[s]; // reference to the count for s
if (c == 0) {
cout << "OK\n";
c = 1;
} else {
cout << s << c << "\n";
++c;
}
}
return 0;
}