A - Babelfish(6.1.2)(6.1使用词典解题实验范例)

Submit Status

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.
代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char dict[100010][2][15];
int n;
bool isblank(char s[])
{
 int k=strlen(s);
 while(--k>=0)
  if(s[k]>='a'&&s[k]<='z')
   return false;
  return true;
}
void swap(char a[],char b[])
{
 char temp[15];
 strcpy(temp,a);
 strcpy(a,b);
 strcpy(b,temp);
}
void sort(int a,int b)
{
    if(a>=b)
        return ;
    int mid;
    char t[15];
    strcpy(t,dict[(a+b)/2][1]);
    int i,j;
    i=a-1;
    j=b+1;
    do
    {
        do
            ++i;
        while(strcmp(t,dict[i][1])>0);
        do
            --j;
        while(strcmp(t,dict[j][1])<0);
        if(i<j)
        {
            swap(dict[i][1],dict[j][1]);
            swap(dict[i][0],dict[j][0]);
        }
    }while(i<j);
    sort(a,j);
    sort(j+1,b);

}
int find(char t[])
{
 int mid,le,ri;
 le=0;ri=n;
 while(le+1<ri)
 {
  mid=(le+ri)/2;
  if(strcmp(dict[mid][1],t)<=0)
   le=mid;
  else
   ri=mid;

 }
 if(strcmp(dict[le][1],t))
   return -1;
 return le;
}

 

int main()
{
 char s[30];

 n=0;
    gets(s);
 while(!isblank(s))
 {
  //strcpy(dict[n][0],s);
  sscanf(s, "%s%s", dict[n][0], dict[n][1]);
 // if((dict[n][0]))
  // break;

  ++n;
 gets(s);
 }
 //sort(0,n-1,dict);
 int min;
 sort(0,n-1);
 while(scanf("%s", s)!=EOF)
 {
  int k=find(s);
  if(k<0)
   printf("%s","eh");
  else
   printf("%s",dict[k][0]);
  printf("\n");
 }
 return 0;
}

你可能感兴趣的:(A - Babelfish(6.1.2)(6.1使用词典解题实验范例))