Careercup - Microsoft面试题 - 5188169901277184
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的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- VUE打包发布后无法访问js、css资源
在vue开发中,本地测试以及测试环境中都没有遇到问题,当发布生产,有虚拟路径时,便出现js.css均报错404: 首先在config的index.js文件中,将assetsPublicPath修改为' ...
- Java图形界面开发—简易记事本
在学习了Java事件之后,自己写了一个极其简单的记事本.用到了MenuBar,Menu,MenuITem等控件,事件包括ActionListener以及KeyListener. 代码如下: ...
- @Valid的坑
@Valid 只能用来验证 @RequestBody 标注的参数,并且要写在 @RequestBody 之前
- Shell脚本 - 用来检查memcache并自动重启生效脚本
#!/bin/sh#check memcache process and restart if downmm_bin="/usr/local/bin/memcached"mm_lo ...
- linux 命令——27 chmod
chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法. 一种是包含字母和操作符表达式的文字设定法: 另一种是包含数字的数字设定法. Linux系统中 ...
- samba性能调优,调优后,性能增加30%
global中增加下面内容. [global] use sendfile = yes write raw = yes read raw = yes max xmit = 655 ...
- PageHelper 记录总条数不正确问题处理
//PageHelper.startPage会返回一个page对象,这个对象在查询结果出来后会把页数,记录总数给page对象,用page.getPages()和getTotal()获取页数和记录总数. ...
- .NET利用RFC连接SAP,查询、读取SAP数据
为黄朴整理!!!!!!!!!!!!!!!!! 在NuGet 添加 sapnco 一个简单的SAPCommand,方法 GetDataTableFromRFCTable 复制于 https://www. ...
- vue axios 攻略
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 特点: 从浏览器中创建 XMLHttpRequest 从 node.js 发出 http 请求 支持 ...
- 第26题:LeetCode572:Subtree of Another Tree另一个树的子树
题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 1: ...