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. ABAP ICON

    1.输出图标的3种方法 WRITE: / '@09@'. "id WRITE: / icon_yellow_light. "name WRITE: / '@S_TL_Y@'. &q ...

  2. <Android 应用 之路> 天气预报(四)

    前言 第二次尝试完成天气预报应用,与上次不同的是,个人感觉这次的Ui不那么丑陋,整体的实用性和界面效果,用户体验相较上一次有所提升,但是还是有很多地方需要完善. 这次使用到的内容比较丰富,包括聚合数据 ...

  3. ArcGIS API for JavaScript开发初探——基础知识

    1.前言 在ArcGIS Web API开发体系中一共有四大类,分别为: ArcGIS API for Flex ArcGIS API for JavaScript ArcGIS API for RE ...

  4. iBrand 教程:Xshell 软件安装过程截图及配置

    下载 教程中使用的相关软件下载网盘: https://pan.baidu.com/s/1bqVD5MJ 密码:4lku 安装 请右键以管理员身份运行进行软件安装,安装过程如下: 配置 安装完成并运行软 ...

  5. JS整数验证

    整数验证 方法1 function ValidatInteger(obj) { var reg = /^[1-9]\d*$/ if (!reg.test($(obj).val())) { $(obj) ...

  6. ORACLE的raw属性

    网上说RAW类型在网络数据传送的时候可以避免字节的字符集转换,在mssql中使用的GUID类型在oracle中对应的也是raw类型(一般是raw(16)),如果此时使用连接查询将raw类型的字段和va ...

  7. 实现Hbase的分页

    作者:R星月 出处:http://www.cnblogs.com/rxingyue 欢迎转载,也请保留这段声明.谢谢! 做一个项目中由于数据量比较大,并且需要定时增量分析,做了hbase的分页.项目中 ...

  8. js点击拉拽轮播图pc端移动端适配

    <div class="content"> <button class="left">left</button> <b ...

  9. zabbix监控系统时间的问题

    分类: 监控 2013-03-19 21:40:11   发现zabbix监控系统时间的一个问题!zabbix监控系统时间用的key是system.localtime,返回当前的系统时间,而配置tig ...

  10. ElasticSearch High Level REST API【1】文档基本操作

    获取ES客户端 ES的提供了四种Java客户端,分别为节点客户端(node client).传输客户端(Transport Client).低级REST客户端.高级REST客户端. 节点客户端作为集群 ...