Official

C - NewFolder(1) Editorial by en_translator


Let \(L=10\) be the maximum length of the strings.

If we search the string every time just as specified in the problem statement, it costs a total of \(\Omega(N^2)\) time, which leads to TLE (Time Limit Exceeded).

Instead, we can use an associative array to store “the number of occurrences of \(S_i\) so far” so that we may determine the string to output in an \(O(L\log N)\) per one string. This way, the problem can be solved in a total of \(O(NL\log N)\) time.

Sample code (C++)

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

int main(){
	int n;
	cin >> n;
	map<string,int>cnt;
	for(int i=0;i<n;i++){
		string s;
		cin >> s;
		if(cnt[s]==0)cout << s << endl;
		else cout << s << "(" << cnt[s] << ")" << endl;
		cnt[s]++;
	}
}

posted:
last update: