百度面试题及我的解答(2)
2009-11-25 23:20
253 查看
接下来开始介绍主程序流程:
使用了GetTickCount()函数测量每一部分代码的运行时间。
第一步为读取数据,并加入Hash表。
第二步就是遍历Hash表。读取每一个冲突链表,并比较其中的所有字符串,得到变位词表。
具体的遍历流程实现在Stepp4TrverseProcess函数中,这个函数的代码最后给出吧。
下一回给出类的定义。
使用了GetTickCount()函数测量每一部分代码的运行时间。
第一步为读取数据,并加入Hash表。
//打开字典文件 ifstream srctxt; char buf[256]={0}; buf[255]=0; srctxt.open("dict.txt"); if (!srctxt.is_open()) { cerr<< "open file dict.txt fail!"<<endl; return -1; } //初始化Hash表,桶设置的很大,不需要再用质数去模运算。 HashMethod1 *pHashMethod = new HashMethod1; if ( !pHashMethod->InitHashTbl(65535) ) { return -1;} //字典文件每行一个单词,读取单词并加入Hash表 while (!srctxt.eof()) { srctxt.getline(buf, 255);; if ( 0 != buf[255] ){ cerr << "buf overflow!" <<endl; buf[255]=0; cerr << buf << endl; return -2; } if ( !pHashMethod->Add(buf) ) { return -1;} } srctxt.close(); tickCount1 = GetTickCount(); cerr << tickCount1- tickCount0 << endl;
第二步就是遍历Hash表。读取每一个冲突链表,并比较其中的所有字符串,得到变位词表。
//遍历Hash表,输出变位词。 tickCount0 = GetTickCount(); ofstream result("result.txt"); pHashMethod->TraverseTbl(&Step4TrverseProcess,result); result.close(); tickCount1 = GetTickCount(); cerr << tickCount1- tickCount0 << endl;
具体的遍历流程实现在Stepp4TrverseProcess函数中,这个函数的代码最后给出吧。
下一回给出类的定义。
相关文章推荐
- 百度面试题及我的解答(5)-1 找出下一步需要优化的内容
- 一道百度的面试题解答
- 百度面试题及我的解答(1)
- 百度面试题及我的解答(5)-2 step5 & step6 代码
- 百度面试题及我的解答(4)
- 百度面试题及我的解答(3)
- 2014百度研发类笔试题【附详细解答及面试题分享】
- 史上最强大的PHP Web面试题(会做可进百度)----我的解答JS版本
- 百度面试题及我的解答(4) 补class HashMethod1的代码
- 百度面试题及我的解答(4) 补 buffer模板类代码
- 百度面试题-蚂蚁过木板-完整解答
- [历年IT面试题]百度2014研发类校园招聘笔试题解答
- 百度面试题及我的解答(5)-0 优化性能
- 百度一道面试题的PHP解答
- 华为Python面试题,排列生成的解法,最优解答
- 算法系列-大数据面试题-在超大文件中找出访问百度次数最多的IP
- 关于淘汰85%面试者的百度开发者面试题
- 百度面试题集锦
- 前端面试题--解答
- 百度开发工程师的面试题(导航部门)