算法习题40:设计最小栈,首尾相连珠子问题,系统设计问题

.百度研发笔试题
引用自:zp15533 4877
1)设计一个栈结构,满足一下条件 min ,push,pop操作 的时间复杂度为O(1)。

2)一串首尾相连的珠子(m个),有N种颜色(N<=10),
设计一个算法,取出其中一段,要 求包 含所有N中颜色,并使长度最短。
并分析时间复杂度与空间复杂度。

3)设计一个系统处理词语搭配问题,比如说 中国 和人民可以搭配,
则中国人民 人民中国都有效。要求:

  *系统每秒的查询数量可能上千次;
  *词语的数量级为10W;
  *每个词至多可以与1W个词搭

当用户输入中国人民的时候,要求返回与这个搭配词组相关的信息。

--------------------------------------------------------

(1):http://blog.csdn.net/ylf13/article/details/12565433

这里给出了几个算法,分别是用空间换时间,改变栈的结构,以及游程编码的方式

(2)这题类似前面字符串查找和谐词语的算法题

http://blog.csdn.net/ylf13/article/details/13503599

关键的就是要有一个哈希表来记录当前记录,其实也类似求数列最大和的问题


而这题的区别就是首尾相接了,这就造成有可能尾部和首部的一些字符组成的字符串是最短的,这里需要考虑进来

而首部应该考虑多少个字符呢?我这里考虑到首部能够组成一个完整的颜色区间的部分

 例如:总共四个颜色

0 1 2 2 3 0 1 2

这里到达a[3]才使得前面构成一个颜色空间,所以考虑的长度就是 0 1 2 2 3 0 1 2  0 1 2 2 

所以然后同样利用hashTable来记录最新位置

脑袋有点乱。。。代码有点乱

//============================================================================
// Name        : FindShortestLen.cpp
// Author      : YLF
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

struct Node{
	int color;
	Node* next;
};


void Insert(Node* &p, int value);
void Print(Node* p);
void Print(Node* p, int start, int end);
void FindShortestLen(Node* head, int color_size, int* start, int* end, int *len);

int main() {
	Node* head = NULL;
	int input=0;
	int color_size;

	cout<<"color_size:";
	cin>>color_size;
	cout<<"colors:";
	while(true){
		cin>>input;
		if(input != -1)
			Insert(head,input);
		else
			break;
	}

	Print(head);
	//头尾接上
	Node* p = head;
	while(p->next != NULL)
		p = p->next;
	p->next = head;


	//找最短
	int start=0,end=10000, len=0;
	FindShortestLen(head, color_size, &start, &end, &len);
	cout<<endl<<(start%len)<<" "<<end%len;
	Print(head, start, end);
	return 0;
}

void Insert(Node* &p, int value){
	if(p != NULL)
		Insert(p->next,value);
	else{
		Node* temp = new Node();
		temp->color = value;
		temp->next = NULL;
		p = temp;
	}
}

void Print(Node* p){
	while(p != NULL){
		cout<<p->color<<" ";
		p = p->next;
	}
}

void Print(Node* p, int start, int end){
	int i =0;
	while(i<start){
		p=p->next;
		i++;
	}
	cout<<"::";
	for(;i<=end;i++){
		cout<<p->color<<" ";
		p = p->next;
	}
}

void FindShortestLen(Node* head, int color_size, int* start, int* end ,int *len){
	int *hashTable = new int[color_size];
	memset(hashTable,-1,color_size*sizeof(int));
	Node* p = head;// 遍历用
	Node* index = NULL; //作为结束判断

	int count = 0;
	int i = 0,min=0;

	hashTable[p->color] = i++;
	count++;
	p = p->next;
	while(true){
		if(count>=color_size || hashTable[p->color] == -1)
			count++;

		if(index==NULL && count == color_size){
			index = p;//循环终止条件
			*end = i;
		}

		hashTable[p->color] = i; //记录最新位置

		// 找到哈系表里最小的值
		int j=0;
		min = hashTable[0];
		for(j=0;j<color_size;j++){
			if(hashTable[j]<min && hashTable[j]!=-1)
				min = hashTable[j];
		}

		if(count >= color_size){
			//如果距离变小,更新
			if((i-min)<((*end) - (*start))){
				*start = min;
				*end = i;
			}
		}

		//是否达到循环结束
		if(p == index && count>color_size)
			break;

		p = p->next;

		i++;
		if(p == head)
			*len = i;
	}
}

color_size:5
colors:1 3 4 2 3 2 2 1 0 3 -1
1 3 4 2 3 2 2 1 0 3 
8 3::0 3 1 3 4 2 

(3)系统设计题

这里是一个词语分词和字典查找问题,

最近才看了一篇文章,说这类题目涉及到大数据存储以及查找问题,可以尝试用用谷歌的三剑客 GFS,MapReduce,BigTable 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值