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 6. 



Updated at 11/11/2013:

After the interview I got know that the O(n^3) solution is not enough to go to the next round. It would have been
better to know before starting implementing the solution unnecessarily ...

------------------------------------------------------------------------

Similar to leetcode
Longest Palindromic Substring Part II
 in my blog, the code is like:

#include <iostream>
#include <map>
#include <algorithm>
#include <limits.h>
#include <assert.h>
#include <string.h>
#include <vector>
using namespace std;
string preprocess(string s) {
string res = "^#";
for (int i = 0; i < s.length(); ++i) {
res += s[i];
res += '#';
}
res += '$';
return res;
}
int getPalindromeNum(string s) {
string str = preprocess(s);
int i, j, len = str.length(), C = 0, R = 0, res = 0, ii; vector<int> T(len + 1, 0), P(len + 1, 0); for (i = 1; i < len; ++i) {
ii = 2*C - i;
P[i] = (R - i) > 0 ? min(P[ii], R-i) : 0;
//bug1: P[i] = (R - i) > 0 ? P[i] : 0
while (str[i + P[i] + 1] == str[i - P[i] - 1])
++P[i];
res += (P[i] + 1) / 2;
//bug2: res += P[i];
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
}
return res;
}
int main() {
//string s = "abcba";
string s = "aaaaa"; int res = getPalindromeNum(s);
return 0;
}

CareerCup Facebook Total number of substring palindrome的更多相关文章

  1. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  2. MYSQL 遭遇 THE TOTAL NUMBER OF LOCKS EXCEEDS THE LOCK TABLE SIZE

    今天进行MySql 一个表数据的清理,经过漫长等待后出现 The total number of locks exceeds the lock table size 提示.以为是table_cache ...

  3. mysql:The total number of locks exceeds the lock table size

    使用mysql InnoDB存储引擎进行大量数据的更新,删除的时候容易引发”The total number of locks exceeds the lock table size”问题,解决方法之 ...

  4. MySQL配置文件路径及‘The total number of locks exceeds the lock table size’问题

    在删除mysql中的数据时,遇到报错: ERROR 1206 (HY000): The total number of locks exceeds the lock table size 查了查,发现 ...

  5. mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法

    1. 问题背景         InnoDB是新版MySQL(v5.5及以后)默认的存储引擎,之前版本的默认引擎为MyISAM,因此,低于5.5版本的mysql配置文件.my.cnf中,关于InnoD ...

  6. Mysql_解决The total number of locks exceeds the lock table size错误

    在操作mysql数据库表时出现以下错误. 网上google搜索相关问题,发现一位外国牛人这么解释: If you're running an operation on a large number o ...

  7. MySQL解决[Err] 1206 - The total number of locks exceeds the lock table size问题

    MySQL解决[Err] 1206 - The total number of locks exceeds the lock table size问题 查看MySQL版本:mysql>show ...

  8. mysql 数据库缓存调优之解决The total number of locks exceeds the lock table size错误

    环境: mysql5.6.2  主从同步(备注:需操作主库和从库) 一.InnoDB表执行大批量数据的更新,插入,删除操作时会出现这个问题,需要调整InnoDB全局的innodb_buffer_poo ...

  9. 【MySQL笔记】mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法

    step1:查看 1.1 Mysql命令行里输入"show engines:"查看innoddb数据引擎状态, 1.2 show variables "%_buffer% ...

随机推荐

  1. STM32 F4 DAC DMA Waveform Generator

    STM32 F4 DAC DMA Waveform Generator Goal: generating an arbitrary periodic waveform using a DAC with ...

  2. redis主从集群搭建及容灾部署(哨兵sentinel)

    Redis也用了一段时间了,记录一下相关集群搭建及配置详解,方便后续使用查阅. 提纲 Redis安装 整体架构 Redis主从结构搭建 Redis容灾部署(哨兵sentinel) Redis常见问题 ...

  3. delphi project of object

    http://www.cnblogs.com/ywangzi/archive/2012/08/28/2659811.html 其实要了解这些东西,适当的学些反汇编,WINDOWS内存管理机制,PE结构 ...

  4. win8、server 2012 清除winsxs文件夹

    使用系统自带的文件清理工具 1.组合键win+x ,选择“命令提示符(管理员)” 复制“dism /online /Cleanup-Image /StartComponentCleanup” 到dos ...

  5. 《完全用Linux工作》作者:王垠

    完全用 GNU/Linux 工作 理解 GNU/Linux 注:本文是清华“牛仔”王垠的“成名作”,在网上引起很大的争议.对他崇拜地五体投地者有,对他嗤之以鼻者也有,总之成了一年多以前Linux 爱好 ...

  6. MyEclipse 2014 for Mac 在Yosemite怎樣安裝

    相信大家都在安裝MyEclipse 2014 for Mac時候會遇到提示虚拟内存为0,,无法安装...小弟找了解決方法...1. 先下載軟件及破解檔案.   http://pan.baidu.com ...

  7. Android ormlite like() function is not working

    //http://stackoverflow.com/questions/7642161/android-ormlite-like-function-is-not-working try { Quer ...

  8. pthread_attr_init线程属性

    转自:http://blog.csdn.net/pbymw8iwm/article/details/6721038 1.线程属性 线程具有属性,用pthread_attr_t表示,在对该结构进行处理之 ...

  9. solc 编译Solidity

    安装 sudo add-apt-repository ppa:ethereum/ethereum sudo apt-get update sudo apt-get install solc 编译 so ...

  10. Material Designer的低版本兼容实现(二)—— Theme

    Theme material主题可以定义为如下形式: @android:style/Theme.Material @android:style/Theme.Material.Light @androi ...