您的位置:首页 > 其它

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

2014-07-24 09:41 344 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息