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;
}



发布了38 篇原创文章 · 获赞 0 · 访问量 340
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览