2014-04-29 03:10

题目:给定一个长字符串S和一个词典T,进行多模式匹配,统计S中T单词出现的总个数。

解法:这是要考察面试者能不能写个AC自动机吗?对面试题来说太难了吧?我不会,所以只写了个KMP用N次的方法。

代码:

 // 18.8 Given a list of words and a piece of text, find out how many times in total, all words appear in the text.
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std; class Solution {
public:
int KMPMatch(const string &word, const string &pattern) {
int index;
int pos;
int result; lw = word.length();
lp = pattern.length();
calculateNext(pattern); index = pos = ;
result = ;
while (index < lw) {
if (pos == - || word[index] == pattern[pos]) {
++index;
++pos;
} else {
pos = next[pos];
} if (pos == lp) {
pos = ;
++result;
}
} return result;
}; ~Solution() {
next.clear();
};
private:
int lw;
int lp;
vector<int> next; void calculateNext(const string &pattern) {
int i = ;
int j = -; next.resize(lp + );
next[] = -;
while (i < lp) {
if (j == - || pattern[i] == pattern[j]) {
++i;
++j;
next[i] = j;
} else {
j = next[j];
}
}
};
}; int main()
{
string text;
unordered_set<string> dict;
Solution sol;
int n, i;
int sum; while (cin >> n && n > ) {
for (i = ; i < n; ++i) {
cin >> text;
dict.insert(text);
}
cin >> text; sum = ;
unordered_set<string>::const_iterator usit;
for (usit = dict.begin(); usit != dict.end(); ++usit) {
sum += sol.KMPMatch(text, *usit);
}
cout << sum << endl; dict.clear();
} return ;
}

《Cracking the Coding Interview》——第18章:难题——题目8的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  9. 《Cracking the Coding Interview》——第18章:难题——题目12

    2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...

  10. 《Cracking the Coding Interview》——第18章:难题——题目11

    2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...

随机推荐

  1. leetcode 136、Single Number

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  2. .net core 2.0以上版本加载appsettings.json

    这里需要的一个关键类: Microsoft.Extensions.Configuration; 可以从nuget包获得 如果缺少该类,会造成无法实例化调用方法: ConfigurationBuilde ...

  3. POJ-2376 Cleaning Shifts---区间覆盖&贪心

    题目链接: https://vjudge.net/problem/POJ-2376 题目大意: farmer John要安排他的牛清理牛棚,一共有T个牛棚要清理,每头牛可以清理相邻的牛棚.比如,一头牛 ...

  4. 线程属性总结 线程的api属性

    http://blog.csdn.net/zsf8701/article/details/7842392 //线程属性结构如下:typedef struct{ int etachstate; //线程 ...

  5. mac home brew install go

    mac利器home brew安装Go 首先你得需要安装home brew和ruby环境(因为home brew依赖ruby) 如果没有请自行到链接安装 准备好之后就开始安装go了 brew updat ...

  6. 旧文备份: 怎样实现SDO服务

    SDO是CANopen协议中最复杂的一部分,带有应答机制,有多种传输方式,并且完整的SDO功能节点需提供1个SDO server和多个SDO client,因此SDO的实现异常困难,协议多种传输方式的 ...

  7. Oracle字符编码与汉字存储长度的处理

    执行如下语句,查看汉字在数据库中所占的字节: select vsize('汉') from dual; 一般情况下,得到的结果大部分为值:2 或 3 一般linux下安装oracle数据库,默认字符编 ...

  8. CSS3一些特殊属性

    (一)-webkit-tap-highlight-color         这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就 ...

  9. Angular之简单的登录注册

    使用Angular实现了一个简单的登录注册的功能........ 涉及到的Angular知识点很少 主要是这个功能的实现...(*^__^*) 嘻嘻…… 里面涉及到的知识点记录: 1.本地存储的操作 ...

  10. docker快速安装jenkins

    用过docker的人,可能真的很难忍受再一步步二进制安装了,好了话不多说,感慨一下jenkins实现自动化发布构建真的很方便. 推荐一个学习的好地方https://m.w3cschool.cn/jen ...