Redian新闻
>
有没有人看到首页那个笑话阿
avatar
有没有人看到首页那个笑话阿# Joke - 肚皮舞运动
j*y
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可以吗?
谢了先。
avatar
m*i
2
关于刘菊花的。。。
avatar
b*8
3
C++ STL map, Java HashMap,都可以啊。
avatar
H*e
4


【在 m****i 的大作中提到】
: 关于刘菊花的。。。
avatar
j*y
5
可以给个用map实现的例子吗?
avatar
k*e
6
笑点在哪里?可能她父母觉得叫翠花太俗,所以取名菊花。一说菊花,就和陶渊明联系
起来了,中国历史上最著名的田园诗人。

【在 m****i 的大作中提到】
: 关于刘菊花的。。。
avatar
j*y
7
从网上抄了一段源代码:
---
#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的头文件了吗?
大家可不可以帮我看看?谢了先。
avatar
E*A
8
留菊花 处女

【在 k****e 的大作中提到】
: 笑点在哪里?可能她父母觉得叫翠花太俗,所以取名菊花。一说菊花,就和陶渊明联系
: 起来了,中国历史上最著名的田园诗人。

avatar
g*e
9
我想问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

avatar
g*e
11
里面也写了那个sample不是hasCode真的implementation。
我想过类似的hash function。至少解这个题是很容易被overflow。any thoughts?

【在 t****o 的大作中提到】
: 可以参考一下 http://en.wikipedia.org/wiki/Java_hashCode()
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。