uva 10282 - Babelfish

Problem C: Babelfish

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".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Output for Sample Input

cat
eh
loops
了解了下字符串哈希,看了很多相关资料但最后还是用quicksort+binsearch
#include<stdio.h>
#include<string.h>
struct node
{char s1[11],s2[11];
}a[100000];
int n=0;
char s[31];
int sort(int l,int r)
{int i=l,j=r;
 char ch1[11],ch2[11];
 if (l>r) return 0;
 strcpy(ch1,a[l].s1);
 strcpy(ch2,a[l].s2);
 while (i<j)
 {while (i<j && strcmp(a[j].s2,ch2)>=0) --j;
  strcpy(a[i].s1,a[j].s1); strcpy(a[i].s2,a[j].s2);
  while (i<j && strcmp(a[i].s2,ch2)<=0) ++i;
  strcpy(a[j].s1,a[i].s1); strcpy(a[j].s2,a[i].s2);
 }
 strcpy(a[i].s1,ch1); strcpy(a[i].s2,ch2);
 sort(l,i-1);
 sort(i+1,r);
}
int find(int l,int r)
{int mid=(l+r)/2;
 if (l>r) return 0;
 if (strcmp(a[mid].s2,s)==0) return mid;
 else
 if (strcmp(a[mid].s2,s)<0) find(mid+1,r);
                    else    find(l,mid-1);
};
int main()
{int i,l,L;
 while (fgets(s,30,stdin))
 {++n;
  if (s[0]=='\n') break;
  for (i=0;s[i]!=' ';i++)
  {a[n].s1[i]=s[i];l=i;}
  a[n].s1[l+1]='\0';
  for (i=l+2;s[i]!='\n';i++)
  {a[n].s2[i-l-2]=s[i];L=i;}
  a[n].s2[L+1]='\0';
 }
 sort(1,n);
 a[0].s1[0]='e';
 a[0].s1[1]='h';
 a[0].s1[2]='\0';
 while (gets(s))
 puts(a[find(1,n)].s1);
 return 0;
}


你可能感兴趣的:(c,input,UP,each,Dictionary,output)