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. 关于在asp.net的web页面中的全局变量问题

    在asp.net的web页面中是不是没有全局变量?有的,在Class类内部的都是,只不过在WebWofm程式中跟WinForm和Console程式有些区别,当页面刷新时,它们的值不会保持,依然会再次初 ...

  2. Android之开源项目view篇

    本文转自:http://www.trinea.cn/android/android-open-source-projects-view/ 主要介绍Android上那些不错个性化的View,包括List ...

  3. 【转载】php程序员:从1.5K到18K 一个程序员的5年成长之路

    昨天收到了心仪企业的口头offer, 回首当初什么都不会开始学编程, 到现在恰好五年. 整天在社区晃悠, 看了不少的总结, 在这个时间点, 我也写一份自己的总结吧. 我一直在社区分享, 所以, 这篇总 ...

  4. js利用数组length属性清空和截短数组

    1.使用length清空数组: 代码如下 复制代码 <script>    var arr1 = ['aaa','bbbb','http://www.111cn.net'];    ale ...

  5. ios警告:Category is implementing a method which will also be implemented by its primary class 引发的相关处理

    今天在处理项目中相关警告的时候发现了很多问题,包括各种第三方库中的警告,以及各种乱七八糟的问题  先说说标题中的问题  Category is implementing a method which ...

  6. sort()的多种用法

    sort()  方法用于对数组的元素进行排序. 一.默认情况 在默认情况下, sort() 方法按升序排列数组项.为了实现排序, sort() 方法会调用每个数组项的 toString() 转型方法, ...

  7. Mybatis-Generator插件自动生成Dao、Model、Mapping相关文件

    最近做项目,mapping 有点多而且容易写错,于是试着用了Mybatis-Generator 插件自动生成 dao, domain  mapping 文件.感觉还挺好用.把相关配置分享,一边以后做项 ...

  8. PCB参数计算神器-Saturn PCB Design Toolkit下载及安装指南

    进行PCB设计,特别是高频高速设计时,难免会涉及到PCB相关参数的计算及设置,如:VIA的过流能力,VIA的寄生电容.阻抗等,导线的载流能力,两相互耦合信号线间的串扰,波长等参数. 这里向大家介绍一款 ...

  9. select的onChange事件问题解决

    一.onChange事件只有在值改变时才可触发,所以必须在每一次选择时(尤其第一次)保证选择的值是改变的! 所以<select name="inv_payee" id=&qu ...

  10. WP开发笔记——程序的退出方法

    Windows Phone程序中,并没有之前的类似于“App.Exit()”之类的函数用来让你退出程序.这是怎么回事儿呢? 很简单,在Windows Phone 7中系统要求配备了硬件的“Back”键 ...