Sweety

Practice makes perfect

导航

 

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

 

 

 

 

sscanf使用时sscanf(ch,"%s%s",str[n].a,str[n].b); ch为要读进去的字符串  “%s%s”为要需要读入字符串的标志,str[n].a,str[n].b要读入字符的变量

其中是默认的遇到空格之后的就读到str[n].b里面的

 

其余的就是思路比较简单的,就是用到几个函数了。。。
 

 

#include<iostream>
#include<cstring>
#include <string>
#include <algorithm>
using namespace std;
struct aa
{
	char  a[20];
	char b[20];
}str[100005];
bool cmp(aa x,aa y)///////配合sort函数使用---比较函数
{
	if(strcmp(x.b,y.b)<0)
		return 1;
	return 0;
	
}
bool isblank (char ch[])//查找是否到了结束条件
{
	if(ch[0]=='\0')
		return 1;
	return 0;
}
int find(char ch[],int n)//二分法(可直接调用)
{
	int l,r;
	l=0;
	r=n;
	int mid=0;
	while(l+1<r)
	{
		mid=(l+r)/2;  
		if(strcmp(str[mid].b,ch)<=0)
			l=mid;
		else
			r=mid;
	};
	return l;
}

int main()
{ 
	int n=0,i;
	char ch[50];
	cin.getline(ch,50);
	while(!isblank(ch))
	{
		sscanf(ch,"%s%s",str[n].a,str[n].b);
		n++;
		cin.getline(ch,50);
	}
	sort(str,str+n,cmp);   ////////使用sort结构体排序
	cin.getline(ch,50);
	while(!isblank(ch))
	{
	int l=find(ch,n);
	cout<<(strcmp(str[l].b,ch)==0?str[l].a:"eh")<<endl;
	cin.getline(ch,50);
	}
	return 0;
}