Babelfish
- Time Limit:
- 3000ms
- Memory limit:
- 65536kB
- 题目描述
- 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 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 is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
- 样例输入
-
dog ogday
-
cat atcay
-
pig igpay
-
froot ootfray
-
loops oopslay
-
-
atcay
-
ittenkay
-
oopslay
- 样例输出
-
cat
-
eh
-
loops
解答:
//利用map 超时了
#include <iostream>
#include <string>
#include <map>
using namespace std;
string s;
map<string,int> in;
int main()
{
in.clear();
int i=0,num=0;
char out[100010][12];
char a[12],b[12],c[12];
char ch=getchar();
while(ch!='\n')
{
int j=0;
while(ch>='a' && ch<='z')
{
a[j++]=ch;
ch=getchar();
}
a[j]='\0';
while(ch>'z' || ch<'a')
ch=getchar();
j=0;
while(ch>='a' && ch<='z')
{
b[j++]=ch;
ch=getchar();
}
b[j]='\0';//每个数组的结束符
num++;
s=b;
in[s]=num;
strcpy(out[num],a);
//while(ch!='\n') ch=getchar();
ch=getchar();
i++;
}
while(scanf("%s",c)!=EOF)
{
s=c;
i=in[s];
if(i>0)
printf("%s\n",out[i]);
else
printf("eh\n");
}
system("pause");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 二分查找
#include<iostream>
using namespace std;
struct pp
{
char a[12],b[12];
}p[100010];
int cmp(void const *c,void const *d)
{
return strcmp(((pp *)c)->b,((pp *)d)->b);
}
int binarysearch(char s[],int k)
{
int low=0,hight=k+1,mid;
while(low<=hight)
{
mid=(low+hight)/2;
if(strcmp(p[mid].b,s)==0) return mid;
else if(strcmp(p[mid].b,s)<0) low=mid+1;
else hight=mid-1;
}
return -1;
}
int main()
{
char ch,s[12];
int i,k,j,t;
ch=getchar();i=0;p[0].a[0]=ch;k=-1;
while(1)
{
k++;
while(ch!=' ') { p[k].a[i++]=ch; ch=getchar(); }
p[k].a[++i]='\0';i=0;
ch=getchar();j=0;
while(ch!='\n') { p[k].b[j++]=ch;ch=getchar(); }
p[k].b[j]='\0';j=0;
ch=getchar();
if(ch=='\n')break;
}
qsort(p,k+1,sizeof(p[0]),cmp);
while(scanf("%s",s)!=EOF)
{
t=binarysearch(s,k);
if(t>=0) printf("%s\n",p[t].a);
else printf("eh\n");
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
const int M=20627;//Hash Table的槽数,取质数对解决冲突较为有效
char English[100000][11],Foreign[100000][11];
struct node//Hash Table中的元素类型
{
int num;//记录英语数组中的标号
struct node *next;
}*link[M]={NULL};//槽中存放一个链表头指针(开散列解决冲突)
int ELFlash(char *key)// UNIX 系统ELF字符串散列函数,对字符串的处理一般使用此函数
{
unsigned long h=0;
while (*key)
{
h=(h<<4)+*key++;
unsigned long g=h & 0xf0000000L;
if (g) h^=g>>24;
h &= ~g;
}
return h%M;
}
int main()
{
char st[30];
int i,k;
node *p;
i=0;
while (gets(st))
{
if (sscanf(st,"%s %s",English[i],Foreign[i])!=2) break;//判断读入词典是否完毕
k=ELFlash(Foreign[i]);//计算Hash函数
p=new node;//建立Hash Table
p->num=i;
p->next=link[k];
link[k]=p;
i++;
}
while (gets(st)!=NULL)
{
k=ELFlash(st);
p=link[k];
while (p!=NULL)//在Hash Table中查找单词
{
if (strcmp(st,Foreign[p->num])==0) break;//若找到则推出
p=p->next;
}
if (p==NULL) printf("eh\n");//没有找到的情况
else printf("%s\n",English[p->num]);
}
return 0;
}
#include<iostream>
using namespace std;
struct node{
node *child[26];
char key[20];
bool d;
}*root;
char s1[20],s2[20],s3[20];
void trie(){
root=new node;
for(int i=0;i<26;i++) root->child[i]=NULL;
root->d=false;
}
void insert(){
node *r=root,*p;
int len=strlen(s3);
for(int i=0;i<len;i++){
if(r->child[s3[i]-'a']==NULL){
p=new node; p->d=false;
for(int j=0;j<26;j++) p->child[j]=NULL;
r->child[s3[i]-'a']=p;
}
r=r->child[s3[i]-'a'];
if(i==len-1){
r->d=true;
strcpy(r->key,s2);
}
}
}
bool search(){
node *r=root;
int len=strlen(s1);
for(int i=0;i<len;i++){
r=r->child[s1[i]-'a'];
if(r==NULL) return false;
if(i==len-1 && r->d){
strcpy(s2,r->key);
return true;
}
}
return false;
}
int main()
{
int i,j,k,len;
trie();
while(true){
gets(s1); if(strcmp(s1,"")==0) break;
for(i=0;s1[i]!=' ';i++) s2[i]=s1[i];
for(i=i+1, j=0, len=strlen(s1);i<len;i++,j++) s3[j]=s1[i];
insert();
}
while(scanf("%s",s1)!=EOF){
if(search()) printf("%s\n",s2);
else printf("eh\n");
}
system("pause");
return 0;
}
#include <stdio.h>
#include <map>
#include <string>
using namespace std;
int main()
{
string s;
char ss[40],s1[20],s2[20];
map <string,string> m;
map <string,string> ::iterator p;
while (gets(ss)) //建立map
{ s=ss;
if (s=="") break;
else
{ sscanf(s.c_str(),"%s %s",s1,s2);
m[s2]=s1;
}
}
while (gets(ss)) //从map查询
{ s=ss;
p=m.find(s);
if ( p!=m.end() ) printf("%s\n",m[s]);
else printf("eh\n");
}
return 0;
}