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的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. HTML5_注册表单的自动聚焦与占位文本

    首先看下面要使用HTML自动聚焦和占位文本的示例代码 1: <!DOCTYPE html> 2: <html> 3: <head> 4: <title> ...

  2. 无需操作系统和虚拟机,直接运行Python代码

    Josh Triplett以一个“笑点”开始了他在PyCon 2015上的演讲:移植Python使其无需操作系统运行:他和他的英特尔同事让解释器能够在GRUB引导程序.BIOS或EFI系统上运行.连演 ...

  3. shell获取本地ip的三种方法

    第一种方法:ifconfig|grep inet |awk '{print $2}'|sed '2d'|awk -F : '{print $2}'第二种方法:ifconfig|grep inet|se ...

  4. Row_Number实现分页(适用SQL)

    1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号的集合 2:再查询该集合的 第 1 ...

  5. (转)Centos5.5安装MONO2.10.8和Jexus 5.0开启Linux平台.net应用新篇章

    注:本文只做本人记录使用,也可供大家参考,有兴趣的可以一起讨论. 安装步骤 1.yum –y update 2.安装Mono源码安装需要的库 yum -y install gcc gcc-c++ bi ...

  6. MongoDB五种树形结构表示法

    MongoDB五种树形结构表示法 第一种:父链接结构 db.categories.insert( { _id: "MongoDB", parent: "Databases ...

  7. linux系统版本查看命令

    发布:theboy   来源:net   [大 中 小] 查看linux系统版本的命令 有如下命令可供参考: # lsb_release -a LSB Version:    :core-3.1-ia ...

  8. 关于promise对象的笔记

    1.promise对象是ECMAScript6的新特性,很多新的JS框架都有它的实现和应用 2.promise常用于异步调用(ajax)中 3.promise主要用于解决回调函数层层嵌套的写法 4.要 ...

  9. 用cookie实现localstorage功能

    在项目中需要利用到html5的localstorage.但在利用这个属性的时候却发现无法达到预定目标.经过不断的检查及排除,最后发现原因所在: 项目中使用的浏览器是支持localstorage的,但是 ...

  10. Oracle 10g RAC 启动与关闭

    一. 检查共享设备 一般情况下,存放OCR和Voting Disk的OCFS2 或者raw 都是自动启动的. 如果他们没有启动,RAC 肯定是启动不了. 1.1 如果使用ocfs2的 检查ocfs2 ...