A - Registration system STL专练

来源:互联网 发布:淘宝怎么看搜索关键词 编辑:程序博客网 时间:2024/05/29 08:45

原题:

A new e-mail service “Berlandesk” is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that’s why they ask you to help. You’re suggested to implement the prototype of site registration system. The system should work on the following principle.

Each time a new user wants to register, he sends to the system a request with his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt 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.

Input
The first line contains number n (1 ≤ n ≤ 105). The following n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters.

Output
Print n lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken.

翻译:

一个新的电子邮件服务系统不久将会面世。现在,这个系统的管理员非常希望这个系统上线的越早越好,于是他找你来帮忙。你被分配到一个管理账号注册的开发部门,这个部门对于账号注册将遵循以下的规则。

每当一个新的用户想要注册账号的时候,他会把他想要注册的用户名告诉系统。如果这个系统里之前没有出现过这个用户名,那么这个用户名就会被导入数据库,从而注册成功。但是,如果这个用户名在数据库中已经存在,系统会自动帮助用户建立一个新的用户名,系统将在用户名的后面从1开始添加数字,一旦发现用户名没有注册过,就会帮助用户直接注册该账号(也就是如果用户想注册name这个用户名,但是系统已经有了,于是系统会自动查找name1是否注册,如果name1也注册了,那么检查name2是否注册,直到找到一个没有用过的用户名,帮用户自动注册)。
Input
输入第一行是一个正整数n,表示有n个用户想要注册。接下来n行,每行一个非空字符串(只由小写字母组成,长度不超过32)表示每个用户想要注册的用户名。1<=n<=10^5

Output
输出有n行,每行一个答案表示对应的用户注册的结果。如果直接注册成功,就输出OK,否则就输出系统帮助用户注册的用户名。

Input

4
abacaba
acaba
abacaba
acab

Output

OK
OK
abacaba1
OK

Input

6
first
first
second
second
third
third

Output

OK
first1
OK
second1
OK
third1

#include <iostream>#include <algorithm>#include <cstdio>#include <string>#include <map>#include <vector>#include <set>using namespace std;int main(){    int n;    map<string ,int> s;    string str;    cin>>n;    while(n--)    {        cin>>str;        if(s[str])            cout<<str<<s[str]<<endl;        else            cout<<"OK"<<endl;        s[str]++;    }    return 0;  }

这就是正解,没错。。。。。
我个智障这题理解麻烦了以为是这样子的
3
f —>OK
f —>f1
f1 —>f2
但其实是这个样子的。。。。
3
f —>OK
f —>f1
f1 —>f11
于是就出来下面这个看似挺有道理的代码了。。。

#include <iostream>#include <algorithm>#include <cstdio>#include <string>#include <map>#include <vector>#include <set>using namespace std;typedef  map<string, int> DATA;DATA data;int main() {    int n;    cin >> n;    DATA::iterator itr;    string name;    for(int i = 0; i < n; i++){        cin >> name;        itr = data.find(name);        if(itr == data.end()){            data[name]=0;            cout << "OK" << endl;        }        else{            if(name[name.length()-1] >= '0' && name[name.length()-1] <= '9'){                string nn=name;                nn.erase(name.length()-1, name.length());                data[name]=data[nn]++ + 1;                name=nn;            }else data[name]++;            char t = '0';            t += data[name];            name += t;            data[name]=0;            cout << name <<endl;        }    }    return 0;}
0 0