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

来源:互联网 发布:淘宝下订单不付款 编辑:程序博客网 时间:2024/06/08 18:51

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 ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops

Hint

Huge input and output,scanf and printf are recommended.


这道题有两种方法可以实现。

第一种:利用快排,二分查找来实现字典查询。

第二种:使用map容器关键字检索。

相比而言,使用第二种方法更为简练。


<span style="font-size:14px;">#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <iomanip>using namespace std;#define MAX 100000+5char en[MAX][15], fg[MAX][15];int n;bool isblank( char str[] ){    int k = strlen(str);    while( --k >= 0 )        if( str[k] >='a' && str[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 QuickSort( char a[][15], char b[][15], int low, int high ){     if(low < high)     {         char elem[15];         strcpy( elem, b[low] );         int l = low, r = high;         while(l < r)         {              while( l < r && strcmp( b[r], elem ) > 0 ) r--;              while( l < r && strcmp( b[l], elem ) < 0 ) l++;              if (l < r)              {                  swap( a[r], a[l] );                  swap( b[r], b[l] );              }         }        QuickSort( a, b, low, r );        QuickSort( a, b, r+1, high );     }     return ;}int dfind( char t[]){    int l, r, mid;    l = 0;    r = n;    while( l + 1 < r )    {        mid = ( l + r ) / 2;        if( strcmp( fg[mid], t ) <= 0 )            l = mid;        else            r = mid;    }    if( strcmp( fg[l], t ) == 0 )        return l;    else        return -1;}int main(){    char s[30];    int i, p;    i = 0;    gets(s);    while( !isblank(s) )    {        sscanf( s, "%s %s", en[i], fg[i] );        i++;        gets(s);    }    n = i;    QuickSort( en, fg, 0, n - 1 );    for(i=0;i<n;i++)        cout<< en[i]<<' '<<fg[i]<<endl;    while( scanf("%s",s ) != EOF )    {        p = dfind( s );        if( p < 0 )            printf( "%s\n", "eh" );        else            printf( "%s\n", en[p] );    }    return 0;}</span><span style="color: blue; font-size: 18pt;"></span>


#include<iostream>#include<map>#include <string>#include <cstdio>using namespace std;int main(){  string str1, str2, str;  map<string, string> m;  map<string, string>::iterator it;  while(1)  {    cin>>str1;    if(getchar()!=' ')      break;    cin>>str2;    m[str2] = str1;  }  do   {    it = m.find(str1);    if(it==m.end())      cout<<"eh"<<endl;    else      cout<<m[str1]<<endl;  }while(cin>>str1);   return 0;}




0 0
原创粉丝点击