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
- A - Babelfish(6.1.2)(6.1使用词典解题实验范例)
- A - Babelfish(6.1.2)(6.1使用词典解题实验范例)
- F - Babelfish解题报告(黄杰)
- F - Babelfish解题报告(map函数)(陈渊)
- A - Babelfish
- Babelfish(6.1.2)(sort结构体排序)(sscanf()基本使用方法)(二分法)
- POJ2503 Babelfish(hash)
- uva10282 - Babelfish(字典)
- Babelfish(map容器)
- POJ2503 Babelfish (map)
- 思想实验(逻辑思维)解题
- poj 2503 Babelfish(字典树或STL水题) 解题报告(百炼2804)
- POJ_2503 Babelfish解题报告.
- F - Babelfish解题报告
- A - Hardwood Species(7.1.1)(利用STL中自带的排序功能编程的实验范例)
- UIReferenceLibraryViewController的使用(调用系统词典)
- POJ 2503 BABELFISH (HASH)
- POJ 2503 Babelfish(Trie)
- 分析Memcached客户端如何把缓存数据分布到多个服务器上
- 2014年7月24日 9点44分
- 【Git】msysgit + TortoiseGit:在 windows 上安装配置版本控制工具 Git 图形化使用
- MISRA工业标准C语言编程规范
- cloudflare对某境外网站加速测试
- A - Babelfish(6.1.2)(6.1使用词典解题实验范例)
- Hadoop1.2.1集群安装三
- 56 阶乘因式分解(一)
- JMeter之正则表达式提取器
- 待读的书籍
- Android开发如何在4.0及以上系统中自定义TitleBar
- $_SERVER['PHP_SELF']和$_SERVER['SCRIPT_NAME']和$_SERVER["QUERY_STRING"],$_SERVER["REQUEST_URI"]
- git revert和git reset的区别
- 单调递增最长子序列--动态规划的经典题目