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. 【AngularJs】---JSONP跨域访问数据传输

    大家会自然想到只有一个字母之差的JSON吧~ JSON(JavaScript Object Notation)和JSONP(JSON with Padding)虽然只有一个字母的差别,但其实他们根本不 ...

  2. 关于DB2 SQL0805N找不到程序包的错误解决办法

    DB2在执行SQL语句的时候会使用内部定义的包(package)来保持不同级别的游标的稳定性, 包的名字就是“ULLID.SYSLH2XX“. DB2 里面默认的时候会创建3个这样的包即SYSLH20 ...

  3. MSI安装程序在Win8/Win10及以上系统中DLL安装问题

    报的错误是: There is a problem with this Windows Installer package. A DLL required for this install to co ...

  4. 零基础Android学习笔记-02 安卓程序生命周期

    一个安卓程序生命周期会经历7中状态,并不一定是每次都全部经历.Create,Start,ReStart,Pause,Resume,Stop,Destory. 重载方法,用helloWorld程序去体验 ...

  5. UIWebView [web视图]

    #import "ViewController.h"#define width_screen self.view.bounds.size.width#define height_s ...

  6. 9款经典华丽的CSS3分享按钮

    如果你经常活跃在一些社交网站上,那么你肯定会看到过很多形式各异的分享按钮,目前由于HTML5和CSS3的普及,很多分享按钮也都应用了CSS3样式,甚至会有很多带有动画的CSS3分享按钮.本文就向大家介 ...

  7. 模板:abs用法

    c语言书本上说,数学函数除了求整数的绝对值函数abs()之外<abs() 定义在stdlib.h中>,其余的函数都在头文件 math.h 中定义,包括对浮点数求绝对值的函数fabs().c ...

  8. 修改eclipse中tomcat的发布路径

    当我们在eclipse部署好tomcat的时候,默认这个项目是部署在eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\t ...

  9. MySQL 密码增强插件

    200 ? "200px" : this.width)!important;} --> 介绍 以前没有太注意MySQL密码安全策略的配置方法,只是人为了将密码设为复杂密码,但 ...

  10. 使用ANT 生成Xfire 客户端端文件

    这里需要用到的JAR包 : XmlSchema-1.1.jar activation-1.1.jar commons-codec-1.3.jar commons-httpclient-3.0.jar ...