由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问个anagram的题目啊
相关主题
how to query in the universal hash table?常见的string hash function
C++ 一问?那个 google hint words 的老题
关于Amazon面试的问题 & 面经问个google面试题
是不是hacking a google interview的题会做就能拿google offer?报一个Amazon internship offer
一道G面经"Hacking a G Interview"怎么有这样低级错?
有几题比较有意思的题,希望大家能给些思路。去脸普做sde难到就是写PHP吗?
发Q家面经computer vision engineer opening
有没有把递归转为非递归的比较通俗易懂的解释。发Facebook intern面经
相关话题的讨论汇总
话题: hash话题: table话题: string话题: letters话题: iterator
进入JobHunting版参与讨论
1 (共1页)
j***y
发帖数: 2074
1
在看Hacking_a_Google_Interview_Handout_2.pdf,里面提到:
Classic Question #6: Data structure for anagrams
Given an English word in the form of a string, how can you quickly find all
valid anagrams for that string (all valid rearrangements of the letters that form
valid English words)? You are allowed to pre‐compute whatever you want to and
store whatever you optionally pre‐compute on disk.
Answer: We want to use a hash table! If your interviewer really hates hash tables (which they sometimes do for some reason), you can use a tree instead. But let's
assume you can use a hash table. Then for the pre‐computing step, go through each
word in the dictionary, sort the letters of the word in alphabetical order (so
"hacking" would become "acghikn") and add the sorted letters as a key in the table
and the original word as one of the values in a list of values for that key. For
example, the entry for "opst" would be the list ["opts", "post", "stop", "pots", "tops", "spot"]. Then, whenever you get a string, you simply sort the letters of the string and look up the value in the hash table. The running time is O(n log n) for sorting the string (which is relatively small) and approximately O(1) for the lookup in the hash table.
大致可以理解这段话,不过用C或者C++怎么实现啊?高手可否给个具体的code实例?
另外,不用hash table,用C++的map可以吗?
谢了先。
b*******8
发帖数: 37364
2
C++ STL map, Java HashMap,都可以啊。
j***y
发帖数: 2074
3
可以给个用map实现的例子吗?
j***y
发帖数: 2074
4
从网上抄了一段源代码:
---
#include
#include
#include
#include
#define ci const_iterator
using namespace std;
int main()
{
typedef string s;
typedef vector vs;
vs l;
copy(istream_iterator(cin), istream_iterator(), back_inserter(l));
map r;
for (vs::ci i = l.begin(), e = l.end(); i != e; ++i)
{
s a = boost::to_lower_copy(*i);
sort(a.begin(), a.end());
r[a].push_back(*i);
}
for (map::ci i = r.begin(), e = r.end(); i != e; ++i)
if (i->second.size() > 1)
*copy(i->second.begin(), i->second.end(), ostream_iterator(
cout, " ")) = "\n";
}
---
有几个地方看不懂。
1. 编译的时候,说“fatal error: boost/algorithm/string.hpp: No such file or
directory”。我用的是tdm-gcc 4.5.1加上codeblocks 10.05的IDE,怎样把boost的库导入进去呢?
2. 在“copy(istream_iterator(cin), istream_iterator(), back_inserter(l
));”里面,说“error: 'istream_iterator' was not declared in this scope”
不是已经加了iostream的头文件了吗?
大家可不可以帮我看看?谢了先。
g*****e
发帖数: 282
5
我想问hash function怎么设计,曾经被问过。没想出简单好用的function。

find all
letters that form
to and
hash tables (which they sometimes do for some reason), you can use a
tree instead. But let's
through each
order (so
in the table

【在 j***y 的大作中提到】
: 在看Hacking_a_Google_Interview_Handout_2.pdf,里面提到:
: Classic Question #6: Data structure for anagrams
: Given an English word in the form of a string, how can you quickly find all
: valid anagrams for that string (all valid rearrangements of the letters that form
: valid English words)? You are allowed to pre‐compute whatever you want to and
: store whatever you optionally pre‐compute on disk.
: Answer: We want to use a hash table! If your interviewer really hates hash tables (which they sometimes do for some reason), you can use a tree instead. But let's
: assume you can use a hash table. Then for the pre‐computing step, go through each
: word in the dictionary, sort the letters of the word in alphabetical order (so
: "hacking" would become "acghikn") and add the sorted letters as a key in the table

t****o
发帖数: 31
g*****e
发帖数: 282
7
里面也写了那个sample不是hasCode真的implementation。
我想过类似的hash function。至少解这个题是很容易被overflow。any thoughts?

【在 t****o 的大作中提到】
: 可以参考一下 http://en.wikipedia.org/wiki/Java_hashCode()
1 (共1页)
进入JobHunting版参与讨论
相关主题
发Facebook intern面经一道G面经
Which C++ compiler do you use on your home computer? (转载)有几题比较有意思的题,希望大家能给些思路。
请教Trading Systems Engineer的职业发展是咋样的? (转载)发Q家面经
公司招人 Computer Programmer,有没有把递归转为非递归的比较通俗易懂的解释。
how to query in the universal hash table?常见的string hash function
C++ 一问?那个 google hint words 的老题
关于Amazon面试的问题 & 面经问个google面试题
是不是hacking a google interview的题会做就能拿google offer?报一个Amazon internship offer
相关话题的讨论汇总
话题: hash话题: table话题: string话题: letters话题: iterator