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

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.


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

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

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

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


<span style="font-size:14px;">#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;

#define MAX 100000+5

char 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT研究僧大师兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值