一道阿里巴巴面试题--海量数据查找

题目:面试官问,我们阿里巴巴公司收到了很多简历,比如说是百万级别的简历,你能不能设计一个算法,让我输入一个姓,程序输出所有这个姓的人,比如我输入张,输出是张三,张四等等

设计思路:

 

设计思路见代码:

/*
This is a free Program, You can modify or redistribute it under the terms of GNU
*Description:阿里巴巴现在收到了百万级别的简历,设计一个系统或者算法,输入某个姓,
如输入wang,把所有有关wang的名字输出,比如输出wang0,wang1,wang2,...

*Language: C++
*Development Environment: VC6.0
*Author: Wangzhicheng
*E-mail: [email protected]
*Date: 2012/10/9
*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef string KeywordType; 
/*
简历类
*/

class Resume {
public:
	KeywordType surname;  //把姓作为关键字
	void *ptr; //指向简历
	string name; //人名
	long no; //简历编号
public:
	Resume(KeywordType surname=0,void *ptr=0,string name=0,long no=0) {
		this->surname=surname;
		this->ptr=ptr;
		this->name=name;
		this->no=no;
	}
	string getName() const {
		return name;
	}
};

const double max=1e5;  //最多的简历数

/*
简历管理类,实现核心算法
*/
class ResumeManage {
private:
	vector* >surnameArray;  //向量中存放着list的地址
	listlist_wang;   //姓名是wang的双向链表
	listlist_li;     //姓名是li的双向链表
	listlist_qian;   //姓名是qian的双向链表
	listlist_zhao;   //姓名是zhao的双向链表
	listlist_sun;    //姓名是sun的双向链表
	listlist_zhang;  //姓名是zhang的双向链表
	//... 还可以增加有关简历的链表

	/*
	此函数随机产生一个string类型的对象
	*/
	string LongToString() {    
		int val=rand()%(long)max;
		int a=val%10;
		char *s=new char[10];
		itoa(a,s,10);
		return static_cast(s);
	}
    /*
	此函数初始化surnameArray
	*/
	void InitSurnameArray() {
		long i;
		srand(unsigned(time(0)));
		for(i=0;i *pos=surnameArray[index];
		list::iterator it;
		if(pos) {
			it=pos->begin();
			while(it!=pos->end()) {
				cout<getName()<surname;
				it++;
			}
		}
		end=clock();
		cout<<"查找时间为"<<(end-start)/CLOCKS_PER_SEC<<"秒"<>surname;
	if(rm.find(surname)==false) {
		cerr<<"姓名不存在,查找失败!"<

 

当max=1e5时,找到全部解

当max=1e6,内存几乎耗尽,程序崩溃

 


 

你可能感兴趣的:(C++算法系列,阿里巴巴,面试,list,string,sun,iterator)