传送门:点击打开链接
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 hisname. If such a name does not exist in the system database, it is inserted into the database, and the user gets the responseOK, confirming the successful registration. If thename already exists in the system database, the system makes up a new user name, sends it to the user as a prompt andalso inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another toname (name1,name2, ...), among these numbers the leasti is found so that namei does not yet exist in the database.
The first line contains number n (1 ≤ n ≤ 105). The followingn 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.
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.
4 abacaba acaba abacaba acab
OK OK abacaba1 OK
6 first first second second third third
OK first1 OK second1 OK third1
题意:给出一些字符串,对于每一个字符串,问前面的字符串中,这个串出现了几次,如果没有出现输出OK
思路:用map<string,int> m来搞一下就行了,每次检查一下m[str]的值并输出就行了。
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
using namespace std;
int main()
{
int n;
map<string, int> m;
scanf("%d", &n);
while (n--)
{
string s;
cin >> s;
if (m[s])
{
cout << s;
cout << m[s] << endl;
m[s]++;
}
else
{
m[s]++;
printf("OK\n");
}
}
return 0;
}
这样写虽然能过,但是时间还是不怎么理想。可以考虑将字符串hash之后,再使用map。
在#include<hash_map> using namespace__gnu_cxx;下面有hash_map,但是我不会,只能手写啦
#include<cstdio>
#include<map>
#include<string>
#include<cstring>
#define uLL unsigned long long
using namespace std;
struct node
{
string s;
int time;
};
int main()
{
int n;
map<uLL, node> m;
scanf("%d", &n);
while (n--)
{
char s[40];
scanf("%s", s);
uLL ha = 0;
int len = strlen(s);
for (int i = 0; i<len; i++) ha = ha * 131 + s[i];
auto it = m.find(ha);
if (it != m.end())
{
printf("%s", s);
printf("%d\n", ++it->second.time);
}
else
{
printf("OK\n");
m.insert(make_pair(ha, (node) { s, 0 }));
}
}
return 0;
}
这样就接近快了5倍。
字典树也可以做,但是要注意下一内存
#include<cstdio>
#include<cstring>
struct node
{
int ch[26];
int num;
} T[500005];
int cnt;
int ans;
void insert(char s[])
{
int len=strlen(s);
int root=1;
for(int i=0; i<len; i++)
{
if(T[root].ch[s[i]-'a']) root=T[root].ch[s[i]-'a'];
else
{
root=T[root].ch[s[i]-'a']=++cnt;
}
}
ans=T[root].num;
T[root].num++;
}
int main()
{
memset(T,0,sizeof(T));
cnt=1;
int n;
scanf("%d",&n);
while(n--)
{
char s[40];
scanf("%s",s);
insert(s);
if(ans) printf("%s%d\n",s,ans);
else printf("OK\n");
}
return 0;
}