Codeforces 4C Registration system MAP和hash的简单应用

传送门:点击打开链接

C. Registration system
time limit per test
5 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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.

Input

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.

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.

Sample test(s)
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

题意:给出一些字符串,对于每一个字符串,问前面的字符串中,这个串出现了几次,如果没有出现输出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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值