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
Sample Output
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;
}
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;
}
相关文章推荐
- A - Babelfish(6.1.2)(6.1使用词典解题实验范例)
- A - Babelfish(6.1.2)(6.1使用词典解题实验范例)
- Babelfish(6.1.2)(sort结构体排序)(sscanf()基本使用方法)(二分法)
- 经典收藏:网络命令行的使用和范例(四)
- 网络命令行的使用和范例(二)
- 经典收藏:网络命令行的使用和范例(二)
- W3 Jmail 使用范例
- 网络命令行的使用和范例(一)
- 使用手机连结电脑,发送端消息的实验步骤
- 经典收藏:网络命令行的使用和范例(十)
- 经典收藏:网络命令行的使用和范例(八)
- W3 Jmail 使用范例
- 经典收藏:网络命令行的使用和范例(十二)
- [分享]FCKEDITOR范例及中文使用说明
- 经典收藏:网络命令行的使用和范例(十一)
- 经典收藏:网络命令行的使用和范例(三)
- 『03网络』 实验一:多功能浏览器的使用和个人Blog的创建和使用
- 使用AspNetPager分页的范例
- 经典收藏:网络命令行的使用和范例(五)
- Oracle恢复实验一:使用添加数据文件之前的控制文件