Divid and Conquer.ppt

  1. 1、本文档共40页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Divide and Conquer Lecture by Hao Wu 一般方法 Master Theorem T(n)=aT(n/b)+f(n) 常见分治法 二分法 三分法 二分搜索 案例1:Babelfish Time Limit: 3000MS Memory Limit: 65536K 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 解题思路 二分查找 因为字典的规模达到100,000的规模,使用线性查找时间肯定不行 核心代码 typedef struct{char src[20],tag[20];}Dict; int cmp(Dict *a,Dict *b){ Return strcmp((*a).tag,(*b).tag);} int BS(int b,int e,char *s){ If (b0||e=n||be||b==estrcmp(D[s].tag,s)) Return -1; int m=(s+e)/2; If (strcmp(D[m].tag,s)==0)return m; If (strcmp(D[m].tag,s)0)return BS(m+1,e,s); return BS(b,m-1,s);} 核心代码(续) int main(){ qsort(D,n,sizeof(D[0]),cmp); while(scanf(“%s”,s)!=EOF) printf(“%s\n”,(r=BS(0,n-1,s))==-1?”eh”:D[r]); Return 0;} 案例6:Raising Modulo Numbers Time Limit:1000MS? Memory Limit:30000K Description People are different. Some secretly read magazines full of interesting girls pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that this market segment was so far underestimated and that there is lack of such games. This kind of game was thus included i

文档评论(0)

sd44055 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档