作者:不详 来源:互联网   酷勤网收集 2007-08-18

摘要
  百度语言翻译机:百度的工程师们是非常注重效率的,在长期的开发与测试过程中,他们逐渐创造了一套独特的缩略语。他们在平时的交谈、会议,甚至在各种技术文档中都会大量运用。为了让新员工可以更快地适应百度的文化,更好地阅读公司的技术文档,人力资源部决定开发一套

题目:

百度语言翻译机



百度的工程师们是非常注重效率的,在长期的开发与测试过程中,他们逐渐创造了一套独特的缩略语。他们在平时的交谈、会议,甚至在各种技术文档中都会大量运用。



为了让新员工可以更快地适应百度的文化,更好地阅读公司的技术文档,人力资源部决定开发一套专用的翻译系统,把相关文档中的缩略语和专有名词翻译成日常语言。



输入要求:

输入数据包含三部分:

1. 第一行包含一个整数N(N<=10000),表示总共有多少个缩略语的词条;



2. 紧接着有N行的输入,每行包含两个字符串,以空格隔开。第一个字符串为缩略语(仅包含大写英文字符,长度不超过10字节),第二个字符串为日常语言(不包含空格,长度不超过255字节);



3. 从第N+2开始到输入结束为包含缩略语的相关文档(总长度不超过1000000个字节)。例:



6

PS 门户搜索部

NLP 自然语言处理

PM 产品市场部

HR 人力资源部

PMD 产品推广部

MD 市场发展部

百度的部门包括PS,PM,HR,PMD,MD等等,其中PS还包括NLP小组。

样例:in.txt



输出要求:

输出将缩略语转换成日常语言后的文档。(将缩略语转换成日常语言,其他字符保留原样)。例:



百度的部门包括门户搜索部,产品市场部,人力资源部,产品推广部,市场发展部等等,其中门户搜索部还包括自然语言处理小组。

样例:out.txt



评分规则:



1.程序将运行在一台Linux机器上(内存使用不作严格限制),在每一测试用例上运行不能超过10秒,否则该用例不得分;



2.要求程序能按照输入样例的格式读取数据文件,按照输出样例的格式将运行结果输出到标准输出上。如果不能正确读入数据和输出数据,该题将不得分;



3.该题目共有4个测试用例,每个测试用例为一个输入文件。各测试用例占该题目分数的比例分别为25%,25%,25%,25%;



4.该题目20分。





注意事项:

1.输入数据是中英文混合的,中文采用GBK编码。

GBK:是又一个汉字编码标准,全称《汉字内码扩展规范》。采用双字节表示,总体编码范围为 8140-FEFE,首字节在 81-FE 之间,尾字节在40-FE 之间,排除xx7F。总计 23940 个码位,共收入 21886 个汉字和图形符号,其中汉字(包括部首和构件)21003 个,图形符号 883 个。



2.为保证答案的唯一性,缩略语的转换采用正向最大匹配(从左到右为正方向)原则。请注意样例中PMD的翻译。



cpp 代码


 


  1. /* 

  2. 我的思路 

  3.  

  4. 1.缩略语 

  5. vector< string >    //用来保存缩略语 

  6. 按string的length排序,来满足"缩略语的转换采用正向最大匹配". 

  7.  

  8. 2.一次性的进行文本替换,以防止替换内容再次被替换 

  9. map<pair<int,int>,string>        //位置范围-缩略语 

  10. vector<pair<int,int>>    //保存位置范围 

  11. map<string,string>    //缩略语 

  12. */  

  13.   

  14. #include <fstream>  

  15. #include <sstream>  

  16. #include <iostream>  

  17.   

  18. #include <vector>  

  19. #include <map>  

  20. #include <list>  

  21.   

  22. #include <string>  

  23.   

  24. #include <algorithm>  

  25. #include <functional>  

  26.   

  27. using namespace std;  

  28.   

  29. #define BEG_END(c)        (c.begin()),(c.end())   

  30.   

  31. typedef string::size_type str_size;  

  32.   

  33. /** 转换string为指定的类型 */  

  34. templatetypename Target, typename Source>  

  35. Target lexical_cast(const Source& arg)  

  36. {  

  37.     Target result;  

  38.     istringstream(arg)>>result;  

  39.     return result;  

  40. }  

  41.   

  42. vector<str_size> find_all(const string& source , const string& aim)  

  43. {  

  44.   

  45.     vector<str_size>    poses;  

  46.   

  47.     str_size pos=0;  

  48.     str_size aim_len=aim.size();  

  49.   

  50.     while ( (pos=source.find(aim, pos)) != string::npos)  

  51.     {  

  52.             poses.push_back(pos);  

  53.             pos += aim_len;  

  54.     }  

  55.   

  56.     return poses;  

  57. }  

  58.   

  59. bool is_long(const string& a , const string& b)  

  60. {  

  61.     return a.length()>b.length();  

  62. }  

  63.   

  64. bool is_first_small(const pair<str_size,str_size>& a , const  pair<str_size,str_size>& b)  

  65. {  

  66.     return a.first<b.first;  

  67. }  

  68.   

  69.   

  70.   

  71. templateclass T,class I>  

  72. bool not_in_scope(I begin,const I& end,const T& aim)  

  73. {  

  74.     for (;begin!=end;++begin)  

  75.     {  

  76.         if (  

  77.             (aim>=(begin->first) ) && (aim<= (begin->first+begin->second) )  

  78.         )return false;  

  79.     }  

  80.     return true;  

  81. }  

  82.   

  83. int main()  

  84. {  

  85.   

  86.     string infile_name="in.txt" , outfile_name="out.txt";  

  87.   

  88.     ofstream outfile(outfile_name.c_str());  

  89.     //ostream& outfile = cout;   

  90.   

  91.     ifstream infile(infile_name.c_str());  

  92.     if (!infile)  

  93.     {  

  94.         cerr<<"Error : can't open input file " <<infile_name<<" .\n";  

  95.         return -1;  

  96.     }  

  97.   

  98.     string line;  

  99.     vector<string> abbr_dict;  

  100.     map<string,string>    abbr_word;  

  101.   

  102.     getline(infile,line);  

  103.     for (int i=lexical_cast<int>(line);i!=0;--i)  

  104.     {  

  105.         getline(infile,line);  

  106.         string abbr,word;  

  107.         istringstream(line)>>abbr>>word;  

  108.         abbr_dict.push_back(abbr);  

  109.         abbr_word[abbr]=word;  

  110.         //cout<<abbr<<' '<<word<<'\n';   

  111.     }  

  112.   

  113.     sort(BEG_END(abbr_dict),is_long);  

  114.   

  115.     while (getline(infile,line))  

  116.     {  

  117.         typedef vector<pair<str_size,str_size> > replace_scope;  

  118.   

  119.         replace_scope    to_replace_scope;  

  120.         map<pair<str_size,str_size>,string>    to_replace;  

  121.   

  122.         for (  

  123.             vector<string>::iterator i=abbr_dict.begin(),end=abbr_dict.end();  

  124.             i!=end;  

  125.             ++i  

  126.         )  

  127.         {  

  128.             vector<str_size>    poses=find_all(line,*i);  

  129.             str_size aim_len=i->size();  

  130.             for (vector<str_size>::iterator j=poses.begin(),end=poses.end();j  

  131.                     !=end;++j)  

  132.             {  

  133.                 pair<str_size,str_size> scope=make_pair(*j,aim_len);  

  134.                 if (not_in_scope(BEG_END(to_replace_scope),*j))  

  135.                 {  

  136.                     to_replace_scope.push_back(scope);  

  137.                     to_replace[scope]=*i;  

  138.                 }  

  139.             }  

  140.         }  

  141.   

  142.         sort(BEG_END(to_replace_scope),is_first_small);  

  143.   

  144.         str_size offset=0;  

  145.   

  146.         for (  

  147.             replace_scope::iterator i=to_replace_scope.begin(),end=to_replace_scope.end();  

  148.             i!=end;  

  149.             ++i  

  150.         )  

  151.         {  

  152.             str_size len=i->second ;  

  153.             string word=abbr_word[to_replace[*i]];  

  154.             line.replace(i->first+offset,len ,word);  

  155.             offset+=word.size()-len;  

  156.         }  

  157.   

  158.   

  159.         outfile<<line<<'\n';  

  160.     }  

  161.   

  162.     return 0;  

  163. }  

分类: IT竞赛比赛 培训考证

上一篇:2006百度之星程序设计竞赛初赛回顾   下一篇:纵观百度之星三年决赛题目,去年的最好,今年的最差,成绩好坏全凭运气