2014-05-12 06:12

题目链接

原题:

Write a function to retrieve the number of a occurrences of a substring(even the reverse of a substring) in a string without using the java substring() method. 

Ex: 'dc' in 'abcd' occurs  times (dc, cd).

题目:写一个函数来统计模式串在文本中出现的次数,不过这次模式的反转也算数。比如“abcd”中dc算出现两次。

解法:按照这种算法,应该是原模式串匹配一次,反转模式串再匹配一次,然后结果加起来乘以二。如果模式串本身是回文串的话,那就只匹配一次。匹配使用KMP算法。

代码:

 // http://www.careercup.com/question?id=5188169901277184
#include <iostream>
#include <string>
#include <vector>
using namespace std; class Solution {
public:
int countWord(const string &word, string &pattern) {
lw = (int)word.length();
lp = (int)pattern.length(); if (lw == || lp == ) {
return ;
} if (lw < lp) {
return ;
} int result = ;
if (isPalindrome(pattern)) {
calculateNext(pattern);
result += KMPMatch(word, pattern);
} else {
calculateNext(pattern);
result += KMPMatch(word, pattern);
reverse(pattern.begin(), pattern.end());
calculateNext(pattern);
result += KMPMatch(word, pattern);
reverse(pattern.begin(), pattern.end());
result *= ;
}
next.clear(); return result;
}
private:
int lw;
int lp;
vector<int> next; bool isPalindrome(const string &s) {
int len = (int)s.length();
int i; if (len <= ) {
return true;
} for (i = ; i < len - - i; ++i) {
if (s[i] != s[len - - i]) {
return false;
}
} return true;
} 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 KMPMatch(const string &word, const string &pattern)
{
int index;
int pos;
int result; 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;
}
}; int main()
{
string word, pattern;
Solution sol; while (cin >> word >> pattern) {
cout << sol.countWord(word, pattern) << endl;
} return ;
}

Careercup - Microsoft面试题 - 5188169901277184的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. arcgis textsymbol overlap

    arcgis  textsymbol   overlap   textsymbol  重叠的问题  du?de?  duration??    arcgis  for  javascript 如何避免 ...

  2. spring mvc 的理解

    1.Spring MVC是什么? Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基 ...

  3. hadoop2.4 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

    在Ubuntu上安装完hadoop2.4以后,使用以下命令: hadoop fs -ls // :: WARN util.NativeCodeLoader: Unable to load native ...

  4. MovieReview—Avengers: Infinity War(复仇者联盟3:无限战争)

    Antagonist? Thanos,the central figure of the Avengers 3,antagonist. Everyone has his own ideals and ...

  5. 【洛谷3648】[APIO2014] 序列分割(斜率优化DP)

    点此看题面 大致题意: 你可以对一个序列进行\(k\)次分割,每次得分为两个块元素和的乘积,求总得分的最大值. 区间\(DPor\)斜率优化\(DP\) 这题目第一眼看上去感觉很明显是区间\(DP\) ...

  6. 优化通过redis实现的一个抢红包流程【下】

    上一篇文章通过redis实现的抢红包通过测试发现有严重的阻塞的问题,抢到红包的用户很快就能得到反馈,不能抢到红包的用户很久(10秒以上)都无法获得抢红包结果,起主要原因是: 1.用了分布式锁,导致所有 ...

  7. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第二节

    原文链接 第二节:第一个内核 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的实验室进行大型并 ...

  8. Jquery中的CheckBox、RadioButton、DropDownList的取值赋值实现代码

    随着Jquery的作用越来越大,使用的朋友也越来越多.在Web中,由于CheckBox. Radiobutton . DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的 ...

  9. C# StreamReader对象

    1.读取文件 输入流用于从外部源读取数据,在很多情况下,数据源可以是磁盘上的文件或网络的某些位置,任何可能发送数据的位置都可以是数据源,比如网络应用程序,web服务,甚至是控制台.StreamRead ...

  10. 第17题:打印1到最大的n位数

    面试题17:打印1到最大的n位数  题目:输入数字n,按顺序打印出从1最大的n位十进制数.比如输入3,则打印出1.2.3一直到最大的3位数即999. 考点: 用字符串或者数组表达一个大数. 思路 1. ...