POJ词典

#include <stdio.h>
#include <string.h>
#include <algorithm>
 
#define MAX_WORD_LEN 11
#define MAX_DICTION_ITEM (100000 + 10)
 
using std::sort;
 
struct Dictionary{
char szWord[MAX_WORD_LEN];
char szEnglish[MAX_WORD_LEN];
};
 
Dictionary diction[MAX_DICTION_ITEM];
 
bool CmpDictionItem(Dictionary one, Dictionary two){
	return strcmp(one.szWord, two.szWord) < 0;
}
 
int FindEnglish(char* pszWord, int nItemNum){
	int nBeg = 0, nEnd = nItemNum - 1;
	int nCmp = 0;
	 
	while (nBeg <= nEnd){
		int nMid = (nBeg + nEnd) / 2;
		nCmp = strcmp(pszWord, diction[nMid].szWord);
		if (nCmp == 0) return nMid;
		else if (nCmp < 0) nEnd = nMid - 1;
		else nBeg = nMid + 1;
	}
	return -1;
}
 
int main(){
	char szStr[30];
	char szWord[MAX_WORD_LEN];
	int nCount = 0;
	int nAnsItem = 0;
	 
	while (fgets(szStr, 29, stdin), szStr[0] != '\n')
	{
	sscanf(szStr, "%s%s", diction[nCount].szEnglish, diction[nCount].szWord);
	++nCount;
	}
	sort(diction, diction + nCount, CmpDictionItem);
	while (scanf("%s", szWord) == 1)
	{
	if ((nAnsItem = FindEnglish(szWord, nCount)) != -1)
	{
	printf("%s\n", diction[nAnsItem].szEnglish);
	}
	else
	{
	printf("eh\n");
	}
	}
	 
	return 0;
}
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;

char str1[100],t1[100],t2[100]; 
string eng,fore,s;
map<string,string> dic;

int main(){
	while(1){
		memset(str1,0,sizeof(str1));
		gets(str1);
		if(strlen(str1)==0) break;
		sscanf(str1,"%s %s",t1,t2);
		eng=t1;
		fore=t2;
		dic[fore]=eng;
	}
	while(scanf("%s",str1)!=EOF){
		s=str1;
		if(dic.find(s)==dic.end()) printf("eh\n");
		else printf("%s\n",dic[s].c_str());
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值