CareerCup Facebook Total number of substring palindrome
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的更多相关文章
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- MYSQL 遭遇 THE TOTAL NUMBER OF LOCKS EXCEEDS THE LOCK TABLE SIZE
今天进行MySql 一个表数据的清理,经过漫长等待后出现 The total number of locks exceeds the lock table size 提示.以为是table_cache ...
- mysql:The total number of locks exceeds the lock table size
使用mysql InnoDB存储引擎进行大量数据的更新,删除的时候容易引发”The total number of locks exceeds the lock table size”问题,解决方法之 ...
- 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 查了查,发现 ...
- 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 ...
- Mysql_解决The total number of locks exceeds the lock table size错误
在操作mysql数据库表时出现以下错误. 网上google搜索相关问题,发现一位外国牛人这么解释: If you're running an operation on a large number o ...
- 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 ...
- mysql 数据库缓存调优之解决The total number of locks exceeds the lock table size错误
环境: mysql5.6.2 主从同步(备注:需操作主库和从库) 一.InnoDB表执行大批量数据的更新,插入,删除操作时会出现这个问题,需要调整InnoDB全局的innodb_buffer_poo ...
- 【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% ...
随机推荐
- ISO 7816-4: Annex A: Transportation of APDU messages by T=0
http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-4_annex-a.aspx Annex A: Transportation ...
- ORA-00600: [kck_rls_check must use (11,0,0,0,0) or lower] 故障解决
一朋友在QQ上问我,说他数据库的pfile 和spfile 都不见了.我问他数据库是10g还是11g的,他说11g,所以我就让他用这个语法来创建spfile了: SQL> create spfi ...
- SQLPrompt_7.2.2.273〖含注册机〗(支持低版本和最高版本SQL2016+VS2015)
SQLPrompt_7.4.1.564[含注册机](支持低版本和最高版本SQL2016+VS2015) http://download.csdn.net/detail/wozengcong/97601 ...
- ASP.NET Web API实践系列01,以ASP.NET Web Form方式寄宿
创建一个空的ASP.NET Web Form项目. 右键项目,添加新项,创建Web API控制器类,TestController. 删除掉TestController默认的内容,编写如下: using ...
- CPU和线程的关系
比如,电脑开了两个程序qq和qq音乐,假设这两个程序都只有一个线程.人能够感觉到CPU切换的频率是一秒一次,假设当前cpu计算速度是1秒1次,那么我们就能明显感到卡顿,当聊天,点击发送按钮时候,qq音 ...
- ArcGIS Pro 切割打印
ArcGIS Pro 切割打印
- 在Android.mk文件中输出打印消息
http://www.xuebuyuan.com/1947880.html 在进行Android NDK的开发当中有时想看看Android.mk文件当中某个变量的值,可以再Android.mk文件当中 ...
- iOS开发UI篇章 15-项目中的常见文件
iOS开发UI篇-常见的项目文件介绍 一.项目文件结构示意图 二.文件介绍 1.products目录:主要用于mac电脑开发的可运行文件.ios开发用不到这个文件 2.frameworks目录主要用来 ...
- 侏罗纪世界2百度云在线观看迅雷下载高清BT下载
原名:Jurassic World: Fallen Kingdom 地区:美国 语言:英语 首播:2018-06-15(中国大陆) / 2018-06-22(美国) 电视台: 类型:动作 / 科幻 / ...
- bat调用TexturePacker更新SpriteSheet
一款游戏会用到很多图片资源,通常我们会使用TexturePacker工具进行图片的拼接.压缩,为了考虑性能问题,单个SpriteSheet的尺寸不会设置的太大(最大1024 * 1024),这样就可能 ...