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. 如何查看win10已激活密钥?查看win10已激活完整密钥的方法!

    如何查看win10已激活密钥?查看win10已激活完整密钥的方法! HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/So ...

  2. JavaScript获取URL参数公共方法

    写一个JavaScript获取URL参数的通用方法,可以把它放到常用方法的JS文件中调用,直接上代码例子,新手可以学习一下! <!DOCTYPE html> <html lang=& ...

  3. c++指针二维数组

    ; int** G; //初始化 G = new int*[N]; ; i < N; i++) G[i] = new int[N]: //删除 ; i < N; i++) delete[] ...

  4. 奇怪的Unrooted Tests错误

    错误如图: 条件如下: Eclipse里的Maven工程. 使用JUnit4(这个是否必须不知,反正我的工程用的4) 修改某个Test类里的方法名,或者增加一个Test方法. 现象: 在MyEclip ...

  5. IOS 获取文本焦点 主动召唤出键盘(becomeFirstResponder) and 失去焦点(退下键盘)

    主动召唤出键盘 - (void)viewDidAppear:(BOOL)animated { // 3.主动召唤出键盘 [self.nameField becomeFirstResponder]; / ...

  6. 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间

    问答 集成Ehcache用来缓存表以后,怎么设置缓存刷新时间  发布于 217天前  作者 老司机  93 次浏览  复制  上一个帖子  下一个帖子  标签: 无 集成Ehcache用来缓存表以后, ...

  7. Luogu [P1248] 加工生产调度

    题目链接 这个题可以贪心 我们首先想:对于所有产品,我们大致可以将其分为三类: ①.在A车间的时间要比B车间长. ②.两者一样. ③.在B车间的时间要比A车间长. 对于这三大类,怎么安排顺序? 可以看 ...

  8. 使用jquery实现获取除this(当前选定)以外的元素

    今天做项目时,要求完成功能评价的分类,即好评,差评,中评.遇到一个问题,如何在选定一个评论类型时,该div颜色改变,其他评论类型的div颜色不变. 在使用$(this).attr()时,表示当前元素的 ...

  9. 函数定义和调用 -------JavaScript

    本文摘要:http://www.liaoxuefeng.com/ 定义函数 在JavaScript中,定义函数的方式如下: function abs(x) { if (x >= 0) { ret ...

  10. Jmeter压力测试工具基本使用

    转:https://blog.csdn.net/envyfan/article/details/42715779