2014-05-03 22:10

题目链接

原题:

Given a dictionary, and a list of letters ( or consider as a string), find the longest word that only uses letters from the string. [I didn't meet this question, what's the best solution?]

题目:给定一个字符串,和一个字典。从字典中找出一个最长的词,并且这个词只使用字符串里的字母。

解法:此人说这题不是自己遇到的,不是自己的面试题你贴它干嘛,难不成又是从别处copy来的?Careercup由于这些复制粘贴党的存在基本已经没有参考价值了。就算是复制粘贴,能否把题目描述清楚!什么叫只用字符串里的字母,能多吗?能少吗?必须刚刚好吗?由于题目信息不足,我姑且认为只要一个单词的字母完全被参照字符串包含,就是满足条件了。在Google题目区我选取了30题来练手,这其中至少有一半是假的。有个发帖者叫“Guy”,如果我有幸见到他本人,我会亲自揍他一顿。因为我明知他发布的一堆Google面试题都是抄来的,还是耐着性子做完了。面试题本身都是好题,但把别人的题目抄来自己发帖就不好了。Careercup这个网站上有很多代码功底很强的人,偶尔能看到他们发言都有茅塞顿开的感觉。但很遗憾,也有很多代码风格极差却经常活跃发言,还大量发布面试题的人。这样的人能够发布大量面试题,只有可能是他们又在geek-for-geek或者glassdoor之类的网站上看见别的题了。他们连on-site之前的电面都不可能通过的,更不用说on-site面试题了。原谅我的吐槽,因为耐着性子做了那么多难度不小的假面试题,实在感觉很窝火,但我仍然相信这些题目能达到练习目的的。

代码:

 // http://www.careercup.com/question?id=5732809947742208
#include <cstring>
#include <string>
#include <unordered_set>
using namespace std; class Solution {
public:
int longestWordInDictionary(unordered_set<string> &dictionary, string letters) {
int i;
int max_len;
int word_len; memset(c1, , MAXLEN * sizeof(int));
word_len = (int)letters.length();
for (i = ; i < word_len; ++i) {
++c1[letters[i]];
} max_len = ;
unordered_set<string>::iterator it;
for (it = dictionary.begin(); it != dictionary.end(); ++it) {
word_len = (int)(*it).length();
if (word_len > (int)letters.length()) {
continue;
} memcpy(c2, c1, MAXLEN * sizeof(int));
for (i = ; i < word_len; ++i) {
if (c2[(*it)[i]] == ) {
break;
}
--c2[(*it)[i]];
}
if (i == word_len) {
max_len = word_len > max_len ? word_len : max_len;
}
} return max_len;
};
private:
const int MAXLEN
int c1[MAXLEN];
int c2[MAXLEN];
};

Careercup - Google面试题 - 5732809947742208的更多相关文章

  1. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  2. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  3. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  4. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  5. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  6. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  7. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  8. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

  9. Careercup - Google面试题 - 5692127791022080

    2014-05-08 22:09 题目链接 原题: Implement a class to create timer object in OOP 题目:用OOP思想设计一个计时器类. 解法:我根据自 ...

随机推荐

  1. Table of Contents - 设计模式

    设计原则 OCP - 开闭原则 SRP - 单一职责原则 DIP - 依赖倒置原则 ISP - 接口隔离原则 LSP - 里氏替换原则 LoD - 迪米特法则 创建型模式 工厂方法模式 抽象工厂模式 ...

  2. 动态创建DataTable总结

    最简单的: DataTable dt = new DataTable(); dt.Columns.Add("id"); dt.Columns.Add("name" ...

  3. js标签放在html的什么位置比较好

    推荐的是js的script标签放在body的末尾,</body>标签之前,包含在body内! <body> <!--其它Html标签--> <script&g ...

  4. CSS之Generator

    这个工具可以,收藏一下.CSS Generator

  5. jQuer中 height scrollTop

    jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用. 注意当浏览器窗口大小改变时(如最 ...

  6. VxWorks 6.9 内核编程指导之读书笔记 -- POSIX

    POSIX能力 VxWorks扩展了POSIX,为了移植,VxWorks提供了额外的POSIX接口作为可选组件.VxWorks实现了POSIX 1003.1(POSIX .1)一些传统接口以及POSI ...

  7. 《Linux shell编程中 diff与vimdif的使用》RHEL6

    linux比较2个文件的区别有两个命令: (1)diff (2)vimdiff cp /etc/grub.conf hello 在hello文件的末尾添加zhangsan 使用diff比较2个文件的区 ...

  8. Azure + vsftpd + ubntu14 + 虚拟用户 遇到的问题:从网上摘抄

    :在Azure安装好ubuntu虚拟机,安装包选择ubuntu14.04 LTS,选择LTS,以便微软对其以后的更好支持,不选就可能技术支持不会很久,现只开一台,端口默认选择22 :打开虚拟机,进入端 ...

  9. emmet(Zen coding)帮助文档

    葵花宝典终可成,半途而废万事空. 官方地址:http://docs.emmet.io/cheat-sheet/ 我导出了pdf版,需要的同学可以下载: 链接:http://pan.baidu.com/ ...

  10. php中的常用魔术方法总结

    以下是对php中的常用魔术方法进行了详细的总结介绍,需要的朋友可以过来参考下 常用的魔术方法有:__Tostring () __Call() __autoLoad() __ clone() __GET ...