Babelfish
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 Hint
Huge input and output,scanf and printf are recommended.
Source |
提示
题意:
输入几组对应的字符串,其中一个是English,另一个是Foreign Language,开始时输入到字典中,然后是根据Foreign Language查询字典,没有时输出eh,有就输出English。(字典里的单词)
思路:
哈希需要动态内存,我的做法是排序+二分。
示例程序
Source Code
Problem: 2503 Code Length: 1243B
Memory: 3132K Time: 625MS
Language: G++ Result: Accepted
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct di
{
char ch[11],id[11];
int bug;
}a[100000];
int cmp(struct di a,struct di b)
{
if(strcmp(a.id,b.id)<1)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int i,i1,top=0,l,r,m;
char s[31];
memset(a,0,sizeof(a));
while(gets(s))
{
if(s[0]=='\0')
{
break;
}
for(i=0;s[i]!=' ';i++)
{
a[top].ch[i]=s[i];
}
a[top].ch[i]='\0';
i++;
for(i1=0;s[i]!='\0';i1++)
{
a[top].id[i1]=s[i];
i++;
}
a[top].ch[i1]='\0';
top++;
}
sort(a,a+top,cmp);
while(scanf("%s",s)!=EOF)
{
r=top-1;
l=0;
while(l<=r)
{
m=(l+r)/2;
if(strcmp(a[m].id,s)==0)
{
printf("%s\n",a[m].ch);
break;
}
else if(strcmp(a[m].id,s)<1)
{
l=m+1;
}
else
{
r=m-1;
}
}
if(l>r)
{
printf("eh\n");
}
}
return 0;
}