百度研发笔试题!新鲜出炉!

zp155334877 2010-10-17 12:53:31
加精
感觉稍微难点的三个:
1)设计一个栈结构,满足一下条件:min,push,pop操作的时间复杂度为O(1)
2)一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
3)设计一个系统处理词语搭配问题,比如说 中国 和人民可以搭配,则中国人民 人民中国都有效。要求:
1)系统每秒的查询数量可能上千次;
2)词语的数量级为10W;
3)每个词至多可以与1W个词搭配

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



...全文
22302 372 打赏 收藏 转发到动态 举报
写回复
用AI写文章
372 条回复
切换为时间正序
请发表友善的回复…
发表回复
灰灰君呀 2012-12-18
  • 打赏
  • 举报
回复
up一哈!膜拜大神啊!
wcycxp 2012-11-30
  • 打赏
  • 举报
回复
好难啊...新手来学习.
wojiaopanpan 2012-08-21
  • 打赏
  • 举报
回复
怎么第二题不考虑首尾相连的问题呢,有可能最短的序列正好横跨你设定的首、尾之间呢……
diqiuzhuanbuzhuan 2012-07-29
  • 打赏
  • 举报
回复
58楼算法可以。
guochaorong 2012-05-21
  • 打赏
  • 举报
回复
我也想要~~
tangyaohong 2011-10-28
  • 打赏
  • 举报
回复
怎么看不见原题呀
skyaspnet 2011-01-28
  • 打赏
  • 举报
回复
是搜索工程师岗位吧?
cwbcwb505 2011-01-28
  • 打赏
  • 举报
回复
哈哈,我跟据前面某位高人的指点,把第二题实现了一下,时间时间复杂度为o(n),空间复杂度为o(m)

#include "stdafx.h"

#include <iostream>
#include <time.h>
using namespace std;

typedef struct stNode
{
int color;
int idx;
stNode* next;
}NODE, *PLIST;

const int ColorNum = 10;
PLIST createlist(int n)
{
PLIST head = NULL;
PLIST currentnode = NULL;
for (int i = 0; i < n; ++i)
{
if (NULL == currentnode)
{
head = new stNode;
head->color = rand() % ColorNum;
head->idx = i;
currentnode = head;
}
else
{
currentnode->next = new stNode;
currentnode = currentnode->next;
currentnode->color = rand() % ColorNum;
currentnode->idx = i;
}
}
currentnode->next = head;
return head;
}

bool findXXX(PLIST head, PLIST* front, PLIST* end)
{
PLIST p1;
PLIST p2;
int colors[ColorNum] = {0};
int CurColorNum;
size_t MinLenth = -1;
p1 = *front = head;
p2 = *end = head->next;
colors[p1->color] += 1;
colors[p2->color] += 1;
if (p1->color == p2->color)
{
CurColorNum = 1;
}
else
{
CurColorNum = 2;
}


int curLenth = 2;
do
{
while (CurColorNum < ColorNum)
{
p2 = p2->next;
if (colors[p2->color] == 0)
{
++CurColorNum;
}
++colors[p2->color];
++curLenth;

if (p2 == p1)
{
return false;
}
}

while (colors[p1->color] - 1 > 0)
{
--colors[p1->color];
p1 = p1->next;
--curLenth;

if (p1 == head)
{
if (MinLenth == -1)
{
return false;
}
else
{
return true;
}
}
}
if (curLenth < MinLenth)
{
*front = p1;
*end = p2;
MinLenth = curLenth;
}
--curLenth;
--CurColorNum;
--colors[p1->color];
p1 = p1->next;
} while (p1 != head);

return true;
}

void printlist(PLIST head)
{
PLIST p = head;
int i = 0;
do
{
cout << p->color << ", ";
if (++i % 10 == 0)
{
cout << endl;
}
p = p->next;
} while (p != head);
}

void printlist(PLIST front, PLIST end)
{
int nlenth = 0;
while (front != end)
{
cout << "idx = " << front->idx << ", color = " << front->color << endl;
++nlenth;
front = front->next;
}

cout << "idx = " << front->idx << ", color = " << front->color << endl;
++nlenth;

cout << "总长度 = " << nlenth << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
srand(time(NULL));
PLIST head = createlist(100);
printlist(head);
cout << endl << endl;
PLIST front;
PLIST end;
findXXX(head, &front, &end);
printlist(front, end);
return 0;
}
Arucart 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 cattycat 的回复:]
不错,第一个用一个普通栈和最小值的栈,每次push的时候,如果比当前最小值小,同时push到最小值的栈。pop的时候如果和最小栈的顶部相同,最小栈也pop。
第二个用类似两个指针扫描的办法,应该可以的。
最后一个考虑内存的话,用位图表可以,再对词组hash就能快一点。
[/Quote]
第一个如果最小值不止一个的话,pop和push几次以后就不对了吧
power721 2010-10-29
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!
随心码 2010-10-29
  • 打赏
  • 举报
回复
找时间看看
q137163819 2010-10-28
  • 打赏
  • 举报
回复
这题目 ,用java怎么实现啊 , 难了一点 , 请大家告知 ,请上传源码
dlutchl 2010-10-28
  • 打赏
  • 举报
回复
学习ing
q137163819 2010-10-28
  • 打赏
  • 举报
回复
第二题的解题
package com.zibet;

import java.util.HashMap;
import java.util.Map;

public class Sub {
/**
* (一次遍历,时间复杂度O(n),需要储存每一颗豆的颜色,空间复杂度O(n))
*/
public Map<Integer, Integer> map = new HashMap<Integer, Integer>();
public int[] shake;
public int index = 0;
public int minSize = Integer.MAX_VALUE;

public Sub(int m, int n, int[] beans) {
shake = new int[m];
for (int i = 0; i < beans.length; i++) {
eatBean(i, n, beans);
}
}

// 颜色
private void eatBean(int i, int n, int[] beans) {
shake[i] = beans[i];
map.put(shake[i], map.get(shake[i]) == null ? 1 : map.get(shake[i]) + 1);
// 满足颜色的种类总数为n
if (map.size() == n) {
while (map.get(shake[index]) > 1) {
popBean();
}
minSize = i - index + 1 < minSize ? i - index + 1 : minSize;
}
}

//
private void popBean() {
map.put(shake[index], map.get(shake[index]) - 1);
index++;
}

public static void main(String[] args) {
int[] ints = new int[] { 0, 1, 2, 3, 2, 1, 3, 3, 3, 4 ,4 };
Sub test = new Sub(11, 5, ints);
System.out.println(test.minSize);
}
}
mijie521 2010-10-25
  • 打赏
  • 举报
回复
好的!
Corepy 2010-10-25
  • 打赏
  • 举报
回复
啥时候的?
skyaspnet 2010-10-25
  • 打赏
  • 举报
回复
学习。。。
this_king 2010-10-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zp155334877 的回复:]
2)类似于暴力求解,不知道还有没有更好的解法:
a)从任意一点开始,首先顺序查找,找到一段珠子,同时包含10种颜色,名为t
b)去掉t的第一颗珠子,并从尾部往后找到第一个与该珠子颜色相同的珠子,加入t,又得到一段珠子,名为 t1
c)重复执行b),直到新选择的一段珠子与t完全重复,退出
……
[/Quote]


这个方法还可以 ,但是没考虑完,如果2个珠子连续的且相同怎么办,比如这个长度为11,那么1和2是相同的,现在取消了1去找一个和1相同的,这样又取消2又去找,做了相同的操作,个人认为时间上不是很好,可以加上一个判断来去掉不必要的查询.做过实际开发的人都知道数据查询的优化效果,海量数据查询是很伤脑筋的
yangzhengrong_xj 2010-10-24
  • 打赏
  • 举报
回复
为了拿分。。。
idealfly 2010-10-24
  • 打赏
  • 举报
回复
谢谢分享,百度难进啊。
加载更多回复(257)

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧