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. Xcode中使用插件

    有两种添加插件的方法.推荐第二种 一.就是在Github上找到你要安装的插件,然后在Xcode完全退出后,打开你要安装的插件,编译就行了,然后完全退出后,重新打开Xcode,会出来这个图 点击load ...

  2. ajax请求简写

    <script type="text/javascript"> function changle() { $.post( "SendMail", / ...

  3. 20150309--gridview

    GridView: 使用代码套用模板,变为DataList的样式,添加<asp:TemplateField>标签,(注意必须加上<Columns>) <asp:GridV ...

  4. UI1_ScrollViewHomeWork

    // // AppDelegate.m // UI1_ScrollViewHomeWork // // Created by zhangxueming on 15/7/13. // Copyright ...

  5. UI1_UISlider与UISegment

    // // ViewController.m // UI1_UISlider与UISegment // // Created by zhangxueming on 15/7/7. // Copyrig ...

  6. UI1_UINavigationController

    // // FourthViewController.h // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. ...

  7. HTML特殊字符编码大全

    HTML特殊字符编码大全:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的 特殊符号大全 ´ © © > > µ ...

  8. QT 多线程程序设计【转】

    QT通过三种形式提供了对线程的支持.它们分别是,一.平台无关的线程类,二.线程安全的事件投递,三.跨线程的信号-槽连接.这使得开发轻巧的多线程Qt程序更为容易,并能充分利用多处理器机器的优势.多线程编 ...

  9. 7款震撼人心的HTML5CSS3文字特效

    1.HTML5像素文字爆炸重组动画特效 今天我们要分享一款基于HTML5技术的文字像素爆炸重组动画特效,我们可以在输入框中指定任意文字,点击确定按钮后,就会将原先的文字爆炸散去,新的文字以像素点的形式 ...

  10. 6款基于SVG的HTML5应用和动画

    1.HTML5 SVG 3D蝴蝶飞舞动画 逼真超酷 这次我们要分享的这款HTML5动画简直就是逆天,利用SVG制作的3D蝴蝶飞舞动画,蝴蝶飞舞动画非常逼真,蝴蝶飞舞的路线是利用SVG构造的.另外,动画 ...