Sunday, September 6, 2009

UserName - SRM 203 Div 2 - TopCoder

string

Given a list of existing usernames, we are to determine whether a newly requested username already figures among them. If so, we must append to it the lowest possible number to make a new username.
I first checked whether the requested username exists among them, if not I return it, otherwise I iterate starting at 1 and at each step I convert the number to string and add it to the requested username, then check if it doesn't exist I return it.
In this approach I have to added two functions, 1 - search the vector for a string 2 - convert integer to string.
But another solutions exists, at first I implement a set of string and insert all the vector elements to it, I have a char candidate[1024] too.
I use sprintf( candidate, "%s%d", userName.c_str(), number ), then I don't have to implement two extra functions.

1 comment: