Careercup - Facebook面试题 - 5671785349513216
2014-05-02 01:05
原题:
bool anaStrStr (string needle, string haystack)
{
} Write a function that takes strings , search returns true if any anagram of string1(needle) is present in string2(haystack)
题目:写一个字符串匹配的函数,但是要求只要能在文本里找到模式的anagram,就返回true。
解法:对于anagram这词的翻译实在是无力吐槽,居然叫“字谜”。所以不翻译反而更省事。因为要匹配的是模式的所有anagram,我们不需要像KMP算法那样计算模式的失配跳转位置,就可以完成O(n)时间内的算法。我们需要随时维护的,是模式串的字符统计,以及当前文本串的字符统计。我们需要一左一右两个iterator,统计两个iterator之间的字串的字母构成情况。开始两者都在0位置,各个字符统计数也都为0。在扫描过程中,如果数量不足,则右端iterator一直向右移动;如果某个字符数量超过了模式串,则左端iterator一直向右移动。这样的话,两个iterator至多扫描完整个文本串,保证算法是严格线性的。
代码:
// http://www.careercup.com/question?id=5671785349513216
#include <iostream>
#include <string>
#include <vector>
using namespace std; class Solution {
public:
bool anaStrStr (string needle, string haystack) {
int len1, len2; len1 = (int)needle.length();
len2 = (int)haystack.length(); if (len1 == ) {
return true;
} else if (len2 < len1) {
return false;
} memset(cn, , * sizeof(int));
memset(ch, , * sizeof(int));
int i, j; cc = ;
for (i = ; i < len1; ++i) {
++cn[needle[i]];
++cc;
} i = ;
j = i;
while (true) {
if (cc == ) {
return true;
} if (i > len2 - len1) {
return false;
} if (ch[haystack[j]] < cn[haystack[j]]) {
++ch[haystack[j]];
--cc;
++j;
} else {
while (i <= j && ch[haystack[j]] == cn[haystack[j]]) {
if (ch[haystack[i]] > ) {
--ch[haystack[i]];
++cc;
}
++i;
}
j = i > j ? i : j;
}
}
};
private:
int cn[], ch[];
int cc;
}; int main()
{
string needle, haystack;
Solution sol; while (cin >> needle >> haystack) {
cout << (sol.anaStrStr(needle, haystack) ? "true" : "false") << endl;
} return ;
}
Careercup - Facebook面试题 - 5671785349513216的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- MVC 页面跳转
想要学习MVC,那么页面跳转就是必不可少的,在此我总结了常见的页面跳转方式,给自己理一下做个备份,也顺便和大家分享一下. 常见页面跳转的:(下面的视图名和控制器中的方法名一样) 在控制器内部跳转: / ...
- Hive通过查询语句向表中插入数据注意事项
最近在学习使用Hive(版本0.13.1)的过程中,发现了一些坑,它们或许是Hive提倡的比关系数据库更加自由的体现(同时引来一些问题),或许是一些bug.总而言之,这些都需要使用Hive的开发人员额 ...
- HTML5桌面通知(Web Notifications)实例解析
先上一段代码,ie不支持,Chrome.fireFox.Opera支持 <!DOCTYPE html> <html> <head> <meta http-eq ...
- iOS 中对各种视图的截屏以及分享
1.一个第三方的工具,主要是对表视图.滚动视图.视图的扩展,用法也很简单 image = [tableview screenshot]; 2.然后将截的图片分享出去,在分享的时候,因为多个地方用到了截 ...
- SQL语句统计每天的数据
按用户注册时间统计每天注册的不同来源.不同状态的用户数量: ), RegisterTime, ) RDate ,--DATEPART(YEAR, RegisterTime) 年 ) END 'AWai ...
- javascript笔记——jquery.each中使用continue和break的方式
jQuery.each中continue的方式是 return true break 的方式是return false
- 《CSS3秘笈》备忘录
第一部分 1. 类名称区分大小写:.special和.SPECIAL不一样 2. :focus 是通过单击或跳格集中在某个地方 3. ::selection 没有单冒号,被选中的文本[ 但是在I ...
- 求单链表倒数第m个结点
问题:求单链表倒数第m个结点,要求不准求链表的长度,也不许对链表进行逆转 解:设置两个指针p和q,p.q指向第一个结点.让p先移动到链表的第m个结点,然后p和q同时向后移动,直到p首先到达尾结点.此时 ...
- ListView Web 服务器控件概述(MSDN)
1: "折叠"图像"展开"图像"复制"图像"复制悬停"图像 全部折叠全部展开 代码:全部 代码:多个 代码:Visual ...
- 【风马一族_Android】代码英语之二 布局文件的Android各个参数
布局文件的Android各个参数 第一类:属性值 true或者 false android:layout _center Hrizontal 水平居中 android:la ...