《Cracking the Coding Interview》——第18章:难题——题目8
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
		Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ... 
- 《Cracking the Coding Interview》读书笔记
		<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ... 
- Cracking the coding interview
		写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ... 
- Cracking the coding interview目录及资料收集
		前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ... 
- Cracking the Coding Interview(Trees and Graphs)
		Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ... 
- 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 ... 
- 二刷Cracking the Coding Interview(CC150第五版)
		第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ... 
- 《Cracking the Coding Interview》——第18章:难题——题目13
		2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ... 
- 《Cracking the Coding Interview》——第18章:难题——题目12
		2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ... 
- 《Cracking the Coding Interview》——第18章:难题——题目11
		2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ... 
随机推荐
- LeetCode OJ Palindrome Number(回文数)
			class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ... 
- SAP CRM WebClient UI和Fiori UI混搭并存
			SAP CRM里有个功能可以创建HANA live report,消费HANA Studio里创建的模型. 最后创建好的report长这个样子: 具体创建步骤可以参考我的博客Step by Step ... 
- 支付宝快速集成ios
			看一下这篇文章,非常不错,并在此感谢这篇文章的作者. 惯例,先写出嵌入支付宝的核心代码 - (IBAction)payWithAli:(UIButton *)sender { //生成订单信息NSSt ... 
- IOS 获取.plist文件的数据
			@property (nonatomic,strong) NSArray *apps; //获取.plist数据 /**获取plist文件的数组数据*/ -(NSArray *)apps{ if( ... 
- P1316 丢瓶盖
			题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ... 
- caffe 输入图像图像加高斯噪声
			这是在frcnn_data_layer的操作,即读图片的操作 if (param.gaussian_noise()) { CHECK(img.type() == CV_8UC3) << & ... 
- python_5_password
			#1.python2中raw_input与python3中的input是相同的,python2中也有input但是别用(不好用,忘记它) #密码是明文的 username=input("us ... 
- Spring Security 之集群Session配置
			1. 新建Maven项目 cluster-session 2. pom.xml <project xmlns="http://maven.apache.org/POM/4.0. ... 
- NoClassDefFoundError: com/ibatis/sqlmap/engine/transaction/external/ExternalTransactionConfig处理
			根据老系统拷贝maven依赖新搭建了一个项目,启动抛异常如下: Caused by: java.lang.NoClassDefFoundError: com/ibatis/sqlmap/engine/ ... 
- 3、SpringBoot+MybatisPlus整合-------代码生成器
			<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</art ... 
