#include <bits/stdc++.h>
#define pp push_back
#define ll long long
#define endl '\n'
#define pb push_back
#define f first
#define s second
#define vl vector<ll>
#define all(v) (v).begin(),v.end()
#define go(i,a,b) for (int i = a; i<b ;i++)
#define gorev(i,a,b) for(int i =a ; i>b ;i--)
#define faster ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
int findFirstMissing(vector<int> arr , int start ,
int end,int first)
{
if (start < end)
{
int mid = (start + end) / 2;
if (arr[mid] != mid+first)
return findFirstMissing(arr, start,
mid , first);
else
return findFirstMissing(arr, mid + 1,
end , first);
}
return start + first;
}
int findFM(vector<int> arr)
{
if(arr[0] != 0)
return 0;
if(arr[arr.size() - 1] == arr.size() - 1)
return arr.size();
int first = arr[0];
return findFirstMissing(arr, 0, arr.size() - 1, first);
}
void dosth() {
int n ;
cin >> n ;
map <string , vector<int> > mp ;
for (int i =0 ;i< n;i++) {
string s ; cin >>s ;
if(!mp[s].size()) {
cout << "ok\n";mp[s].pb(0) ;
string tt ="" ; int acc =0 ;
for (int j =0 ;j<s.length() ;j++){
if(isdigit(s[j])){
int d = s[j]-'0' ;
acc*=10 ;
acc+=d ;
}
else {
tt+=s[j] ;
}
}
if(acc!=0)
mp[tt].pb(acc) ;
//cout << tt <<acc << " " <<endl; ;
}
else {
sort(all(mp[s])) ;
int t = findFM(mp[s]) ;
if(t==0) {
cout <<"ok" <<endl ;
mp[s].pb(0) ;
}
else{
string temp = s + (char)(48+t) ;
mp[temp].pb(0) ;
mp[s].pb(t) ;
cout << temp <<endl ;
}
}
}
}
int main () {
faster ;
int t =1;
//cin >>t ;
while(t--){
dosth() ;
}
return 0 ;
}
Copy