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. php中一串数子的转化

    echo "<meta charset=utf-8>"; echo "笔试题测试"; $str = "123456780"; $ ...

  2. External Table

    CREATE TABLE AS SELECT,使用Oracle9i的External Table  Oracle 9i 的一项新特性就是 External Table,它就象通常的数据库表一样,拥有字 ...

  3. (转载)一步一步学Linq to sql系列文章

    现在Linq to sql的资料还不是很多,本人水平有限,如果有错或者误导请指出,谢谢. 一步一步学Linq to sql(一):预备知识 一步一步学Linq to sql(二):DataContex ...

  4. Cisco基本命令配置

    实验一 路由器的基本命令操作 1 实验目标 ü 熟悉路由器的命令行操作 ü 能够使用命令行帮助 ü 能够查看路由器接口信息 ü 能够产看路由器配置信息 ü 能够配置以太网接口 ü 能够配置广域网接口 ...

  5. 【原创】Tomcat集群环境下对session进行外部缓存的方法(1)

    BJJC网改版, 计划将应用部署在tomcat集群上,集群的部署方案为Apache+Tomcat6,连接件为mod_jk,其中开启了session复制和粘性session.计划节点数为3个. 到这,或 ...

  6. asp.net 客户邮件群发功能 SendMail 发送静态化html

    背景:现在几乎每个企业都要用到邮箱,而在大客户量情况下,为我们的不同等级的客户送上节日关怀,以及把我们的喜讯.新品通知到他们是我们急需解决的问题.效果如图 思路: 1.静态化网页模版,首先考虑需要发送 ...

  7. webSphere集群部署主要步骤

    1.系统管理-节点,添加本机节点和另外一台机器的节点2.建立集群服务cluster,添加成员节点3.将应用部署到集群服务cluster4.将数据库源分别建立到节点作用域5.后续步骤参照安装手册 注意事 ...

  8. 1 . Robberies (hdu 2955)

    The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually g ...

  9. Netfilter深度解剖

         在网络上发现这个Netfilter写的很好的系列文章,为了便于后期寻找,特此标注下博客地址,感谢大大神提供.     ---------------------------分割线开始---- ...

  10. ADO.NET笔记——读取二进制大对象(BLOB)

    相关知识: 在SQL Server中,一般情况下,每行数据的总长度不能超过8K字节.因此,下列数据类型的长度,也不能超过8K字节:binary,char(),nchar(),varchar(),nva ...