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. 集合类学习之HashMap

    一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了不同步和允许使用 null 之外,HashMap  ...

  2. 总结一下block的几种常用方法

    一.block的简单介绍 block相当于一个参数,有参数的名,有参数的值,有参数的使用,当使用block的时候,它又相当于一个函数,会回调函数体. 声明:void (^block)(void); 实 ...

  3. UI4_UIWebView

    // // ViewController.m // UI4_UIWebView // // Created by zhangxueming on 15/7/7. // Copyright (c) 20 ...

  4. php折线图 布局图

    例子1: 1 <?php require_once("../conf.php"); ?> <!DOCTYPE HTML> <html> < ...

  5. Stack Overflow 2016最新架构探秘

    这篇文章主要揭秘 Stack Overflow 截止到 2016 年的技术架构. 首先给出一个直观的数据,让大家有个初步的印象. 相比于 2013 年 11 月,Stack Overflow 在 20 ...

  6. 分布式PostGIS系列【2】——pgpool-II

    一.pgpool-II简介 二.pgpool-II安装与配置 三.分布式Postgis性能测试

  7. 深度模拟java动态代理实现机制系类之三

    这里的内容就比较复杂了,要实现的是对任意的接口,对任意指定的方法,以及对任意指定的代理类型进行代理,就更真实的模拟出java虚拟机的动态代理机制 罗列一下这里涉及的类.接口之间的关系,方便大家学习.1 ...

  8. Debian--changelog

    以前就研究过debian安装包的问题,当时也没有做相关方面的记录,当时也没有完全研究明白,现在重新研究下,现在写下我的一些笔记,等我研究明白了,我会整理出来,出个系列博客,有兴趣的同学,也可以去看E文 ...

  9. WP开发笔记——页面传参

    WP APP页面与页面之间参数的传递可以通过程序的App类设置全局变量. 由于App 类继承自Application类,而通过Application的Current属性可以获取到与当前程序关联的App ...

  10. BootStrap简介及应用要点

    BootStrap简介 BootStrap是基于HTML.CSS和JavaScript的框架,使你只需要写简单的代码就可以很快的搭建一个还不错的前端框架,他是后端程序员的福音,使他们只需要专注业务逻辑 ...