vua 10282 - Babelfish(Hash、map)

点击打开链接


题意:100000个字的字典。


1、直接用STL中的map 0.532sAC

2、Hash 0.115sAC


#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
int const MAX_SIZE = 11, MAX_N = 100000;
map<string,string> dict;

void print(string s)
{
	if(dict.find(s) != dict.end())
		cout << dict[s] << endl;
	else 
		cout << "eh" << endl;
}

int main()
{
	string s1, s2;
	while(cin >> s1 && cin.peek()!='\n')
	{
		cin >> s2;
		dict[s2] = s1;
	}
	print(s1);
	while(cin >> s1)
		print(s1);
	
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int const MAX_SIZE = 11, MAX_N = 100000, MAX_HASH = 100003;
int n;
char s1[MAX_N][MAX_SIZE], s2[MAX_N][MAX_SIZE];
int head[MAX_HASH], nextu[MAX_HASH];

void init_lookup_tabel(){ memset(head, 0, sizeof(head));}

int get_hash(char *s)
{
	int seed = 131;
	int hash = 0;
	while(*s)
		hash = hash * seed + *s++;
	return (hash & 0x7fffffff) % MAX_HASH;
}

int find(char *s)
{
	int hash = get_hash(s);
	int u = head[hash];
	while(u)
	{
		if(!strcmp(s2[u], s))
			return u;
		u = nextu[u];
	}
	return -1;
}

void try_to_insert(int i)
{
	int hash = get_hash(s2[i]);
	nextu[i] = head[hash];
	head[hash] = i;
}

void print(char *s)
{
	int i = find(s);
	if(i != -1)
		printf("%s\n", s1[i]);
	else 
		printf("eh\n");
}

int main()
{
	n = 1;
	char ss1[MAX_SIZE], ss2[MAX_SIZE];
	while(scanf("%s", ss1) && cin.peek()!= '\n')
	{
		scanf("%s", ss2);
		strcpy(s1[n], ss1);
		strcpy(s2[n], ss2);
		try_to_insert(n++);
	}
	print(ss1);
	while(scanf("%s", ss1) != EOF)
		print(ss1);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值