2014-05-02 08:29

题目链接

原题:

Write a function for retrieving the total number of substring palindromes.
For example the input is 'abba' then the possible palindromes= a, b, b, a, bb, abba
So the result is . Updated at //:
After the interview I got know that the O(n^) solution is not enough to go to the next round. It would have been better to know before starting implementing the solution unnecessarily ...

题目:给定一个字符串,统计所有回文子串的个数。O(n^3)的算法是明显的暴力算法,当然要被淘汰了。

解法1:一个简单的优化,是O(n^2)的。从每个位置向两边计算有多少个回文串。这样可以用O(1)空间,O(n^2)时间完成算法。

代码:

 // http://www.careercup.com/question?id=5177378863054848
#include <iostream>
#include <string>
#include <vector>
using namespace std; int countPalindrome(const string &s)
{
int n = (int)s.length();
if (n <= ) {
return n;
} int i, j;
int res;
int count; res = ;
for (i = ; i < n; ++i) {
j = ;
count = ;
while (i - j >= && i + j <= n - && s[i - j] == s[i + j]) {
++count;
++j;
}
res += count; j = ;
count = ;
while (i - j >= && i + + j <= n - && s[i - j] == s[i + + j]) {
++count;
++j;
}
res += count;
} return res;
} int main()
{
string s; while(cin >> s) {
cout << countPalindrome(s) << endl;
} return ;
}

解法2:有个很巧妙的回文串判定算法,叫Manacher算法,可以在O(n)时间内找出最长回文字串的长度。这题虽然是统计个数,同样可以用Manacher算法搞定。Manacher算法中有个很重要的概念,叫“最长回文匹配半径”,意思是当前最长回文子串能覆盖到的最靠右的位置。每当我们检查的中心位置处于这个半径之内时,就可以和另一边的对称点进行参照,减少一些重复的扫描。如果处于半径之外,就按照常规的方式向两边扫描了。Manacher算法的思想一两句话很难说清楚,在此提供一个链接吧:Manacher算法处理字符串回文

代码:

 // http://www.careercup.com/question?id=5177378863054848
// Modified Manacher Algorithm
#include <algorithm>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
using namespace std; class Solution {
public:
long long int countPalindrome(const string &s) {
int n = (int)s.length(); if (n <= ) {
return n;
} preProcess(s);
n = (int)ss.length();
int far;
int far_i; int i; far = ;
c[] = ;
for (i = ; i < n; ++i) {
c[i] = ;
if (far > i) {
c[i] = c[ * far_i - i];
if (far - i < c[i]) {
c[i] = far - i;
}
} while (ss[i - c[i]] == ss[i + c[i]]) {
++c[i];
} if (i + c[i] > far) {
far = i + c[i];
far_i = i;
}
} long long int count = ;
for (i = ; i < n; ++i) {
count += (c[i] + (i & )) / ;
} ss.clear();
c.clear(); return count;
}
private:
string ss;
vector<int> c; void preProcess(const string &s) {
int n;
int i; n = (int)s.length();
ss.clear();
// don't insert '#' here, index may go out of bound.
ss.push_back('$');
for (i = ; i < n; ++i) {
ss.push_back(s[i]);
ss.push_back('#');
}
c.resize(ss.length());
};
}; int main()
{
string s;
Solution sol;
const int big_n = ; s.resize(big_n);
for(int i = ; i < big_n; ++i) {
s[i] = 'a';
} clock_t start, end;
start = clock();
cout << sol.countPalindrome(s) << endl;
end = clock();
cout << "Runtime for test case of size " << big_n << ": "
<< (1.0 * (end - start) / CLOCKS_PER_SEC)
<< " seconds." << endl; while (cin >> s) {
cout << sol.countPalindrome(s) << endl;
} return ;
}

Careercup - Facebook面试题 - 5177378863054848的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  8. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

  9. Careercup - Facebook面试题 - 5188884744896512

    2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...

随机推荐

  1. 用jquery 实现 超出字符 截断加上省略号并且可以提示全部内容

    1.test.jsp Java代码 <%@ page language="java" pageEncoding="UTF-8"%> <html ...

  2. JNA结构体参数传递,Java数组

    JNA以结构体数组为参数进行调用: ////// C++ // student 结构体定义 typedef struct { int age; char name[20]; }Student; //  ...

  3. CPU 硬盘性能到底相差多少

    本文以一个现代的.实际的个人电脑为对象,分析其中CPU(Intel Core 2 Duo 3.0GHz)以及各类子系统的运行速度——延迟和数据吞吐量.通过粗略的估算PC各个组件的相对运行速度,希望能给 ...

  4. jQuery简单邮箱验证

    function chekmail() { var szReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; ...

  5. C# tostring 格式化输出 (转)

    C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...

  6. Linux中shell命令的用法和技巧

    使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和l技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务.于是,我开始在一个文本文件里记录这些用法,并 ...

  7. php中json_decode返回数组或对象的实例

    1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...

  8. Newtonsoft.Json 基本用法

    Newtonsoft.Json 是.net 开源的一个json格式处理类库 官方网站:http://json.codeplex.com/ 在使用的项目引用Newtonsoft.Json库.平常使用的方 ...

  9. spring注解中使用properties文件

    一.只读取单个 properties 文件 1.在 spring 的配置文件中,加入 引入命名空间: xmlns:util="http://www.springframework.org/s ...

  10. iPhone Tableview分批显示数据

    //非原创   iPhone Tableview分批显示数据是本文要介绍的内容,主要讲解的是数据的显示.iPhone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10 ...