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,布布扣,bubuko.com

时间: 2024-10-26 00:08:57

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

Total Number of Unicorn Companies: 188

https://www.cbinsights.com/research-unicorn-companies Total Number of Unicorn Companies: 188 Total Cumulative Valuation: $647B tions) The Unicorn Company List Company Valuation ($B) Date Joined Country Industry Select Investors Decolar $1 3/10/2015 A

【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%"里查看innodb_buffer_pool_size的数值,默认是8M(太小,需要改大一点!) step2:找配置文件,修改innodb_buffer_pool_size=64M 2.1 在linux里配置文件是my.cnf,windows里是my.ini(注:不是my-default.ini).

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

在操作mysql数据库表时出现以下错误. 网上google搜索相关问题,发现一位外国牛人这么解释: If you're running an operation on a large number of rows within a table that uses the InnoDB storage engine, you might see this error: ERROR 1206 (HY000): The total number of locks exceeds the lock ta

total number of hops

If you can hop 1, 2, or 3 steps at a time, calculate the total number of possible combinations for `n` steps. I thought of it as follows, let S(n) be the # of combinationsto go n stepsa) to go n+3 is the S(n+2) + the hop with distance 1 b) to go n+3

[Daily Coding Problem] Find the total number of solutions of a linear equation of n variables

Given a linear equation of n variables, find the total number of non-negative integer solutions of it. All coefficients are positive. Example: input: x + 2 * y = 5 output: 3,  the 3 possible integer solutions are x = 1, y = 2; x = 3, y = 1; x = 5, y

MYSQL碰到The total number of locks exceeds the lock table size 问题解决记录

解决记录如下: 在mysql里面进行修改操作时提示:The total number of locks exceeds the lock table size ,通过百度搜到innodb_buffer_pool_size过小: 打开mysql 命令框 输入 show variables like "%tmp%"; 查看innodb_buffer_pool_size,输入SET GLOBAL innodb_buffer_pool_size=67108864; 完成之后再次使用show v

The total number of locks exceeds the lock table s

InnoDB表执行大批量数据的更新,插入,删除操作时会出现这个问题,需要调整InnoDB全局的innodb_buffer_pool_size的值来解决这个问题 SHOW GLOBAL VARIABLES LIKE "%buffer_pool%" 可以通过编辑/etc/my.cnf,添加下面的语句来修改缓存大小: innodb_buffer_pool_size = 2G

hdu 5062 Beautiful Palindrome Number(Bestcodeer Round #13)

Beautiful Palindrome Number                                                                 Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 116    Accepted Submission(s): 82 Problem Description

[Coding Made Simple] Coin Changes Number of ways to get a total

Given coins of certain denominations and a total, how many ways these coins can be combined to get the total. Dynamic Programming solution State: T[i][j]: given the first i coins, the total number of ways these coins can be combined to get the total