POJ2503 Babelfish

//6_1_2:	Babelfish 英文单词和外语单词之间的转换 POJ2503
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxc = 10 + 5;
const int maxn = 100000 + 5;
int n;
struct info
{
	char org[maxc];
	char trs[maxc];
}dict[maxn];
bool isEmpty(char str[])
{
	int i = 0;
	while(str[i])	
	{
		if(str[i] >= 'a' && str[i] <= 'z')	return false;
		i++;
	}
	return true;
}
void quick_sort(int st,int ed)			//快排算法
{
	int i = st,j = ed;
	info temp;
	if(st < ed)
	{
		temp = dict[st];
		while(i != j)
		{
			while(j > i && strcmp(dict[j].trs,temp.trs) >= 0)	j--;
			dict[i] = dict[j];
			while(i < j && strcmp(dict[i].trs,temp.trs) <= 0)	i++;
			dict[j] = dict[i];
		}
		dict[i] = temp;
		quick_sort(st,i - 1);
		quick_sort(i + 1,ed);
	}
}
int find(char trs[])
{
	int low = 0,hig = n,mid,ans;
	while(low <= hig)
	{
		mid = (low + hig) / 2;
		ans = strcmp(trs,dict[mid].trs);
		if(ans == 0)		return mid;
		else if(ans < 0)	hig = mid - 1;
		else if(ans > 0)	low = mid + 1;
	}
	return -1;
}
int main()
{
	char str[maxc * 2];
	int i = 0;
	gets(str);
	while(!isEmpty(str))
	{
		sscanf(str,"%s%s",dict[i].org,dict[i].trs);
		i++;
		gets(str);
	}
	n = i - 1;
	quick_sort(0,n);
	while(scanf("%s",str) != EOF)
	{
		i = find(str);
		if(i < 0)	printf("eh\n");
		else		printf("%s\n",dict[i].org);
	}
	return 0;
}
/*测试结果:通过POJ2503检测
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
cat
ittenkay
eh
oopslay
loops
^Z
请按任意键继续. . .
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值