转载地址

https://www.cnblogs.com/AlvinZH/p/8527668.html#_label5


题目详情

给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。

具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串。

示例 1:

输入: "abc"
输出: 3
解释: 三个回文子串: "a", "b", "c".

示例 2:

输入: "aaa"
输出: 6
说明: 6个回文子串: "a", "a", "a", "aa", "aa", "aaa".

注意:

  1. 输入的字符串长度不会超过1000。

题目分析

一个小问题,子串(Substring)、子数组(Subarray)和子序列(Subsequence)的区别:子串和子数组是等同的,特点是连续的,比如[1,2,3]的子串有(1), (2), (3), (1,2), (2,3), (1,2,3)。而子序列不一定相邻,但相对顺序一致,比如(1,3)是[1,2,3]的一个子序列。

方法有很多种,简单讲一些。


方法一: DP

一开始定义DP[i][j]为i、j之间的回文子串数,很是麻烦,还需要另外的数组记录子串[i, j]是否是回文的。其实没有必要,直接将DP[i][j]定义成子串[i, j]是否是回文串。外循环 i从 n−1 往 0 遍历,内循环 j 从 i 往 n−1 遍历,若s[i]==s[j]:

若i==j,则dp[i][j]=true;

若i和j是相邻的,则dp[i][j]=true;

若i和j中间只有一个字符,则dp[i][j]=true;

否则,检查dp[i+1][j-1]是否为true,若为true,那么dp[i][j]就是true。

前三条可以合并,即 j−i≤2。求得dp[i][j]真值后,如果其为true,最终结果res++。

时间复杂度:O(n^2)。

方法一参考代码:

class Solution {
public:
int countSubstrings(string s) {
int len = s.size(), res = 0;
vector<vector<bool>> dp(len, vector<bool>(len, false));
for (int i = len - 1; i >= 0; --i) {
for (int j = i; j < len; ++j) {
dp[i][j] = (s[i] == s[j]) && (j - i <= 2 || dp[i + 1][j - 1]);
if (dp[i][j]) ++res;
}
}
return res;
}
};

方法二:回文中心法

本题可以不用DP,而是采用一种巧妙的方法:回文中心法。什么意思呢?考虑不同的回文中心,往两边扩散,求得回文数。需要考虑两种情况:如果是奇数长度回文串,了么回文中心为最中间的一个字符;如果是偶数长度回文串,这回文中心为最中间的两个字符。

每个回文子串只有一个回文中心,所以这种方法不会重复计算,也不会漏算。

时间复杂度:O(n^2)。

方法二参考代码:

class Solution {
public:
int countSubstrings(string s) {
int len = s.size(), res = 0;
for (int i = 0; i < len; ++i) {
int mid1 = i, mid2 = i;//奇数
while (mid1 >= 0 && mid2 < len && s[mid1] == s[mid2]) {
--mid1; ++mid2; ++res;
} mid1 = i, mid2 = i+1;//偶数
while (mid1 >= 0 && mid2 < len && s[mid1] == s[mid2]) {
--mid1; ++mid2; ++res;
}
}
return res;
}
};

方法三:“马拉车”算法

神奇的算法,先马一下,学会再写上。听说时间复杂度是 O(n)。

好了,学到了,请参考:什么是马拉车算法?

利用马拉车算法,可以得到所有情况下的最大半径,以s[i]为中心,RL[i]为半径的回文串中含有的字回文串数目是 RL[i]/2 个。

方法三参考代码:

class Solution {
public:
int countSubstrings(string s) {
//预处理
string t = "#";
for (int i = 0; i < s.size(); ++i) {
t += s[i];
t += "#";
} vector<int> RL(t.size(), 0);
int MaxRight = 0, pos = 0;
int res = 0;
for (int i = 0; i < t.size(); ++i) {
RL[i] = MaxRight > i ? min(RL[2 * pos - i], MaxRight - i) : 1; while (i-RL[i] >=0 && i+RL[i] < t.size() && t[i + RL[i]] == t[i - RL[i]])//扩展,注意边界
++RL[i];
//更新最右端及其中心
if (MaxRight < i + RL[i] -1) {
MaxRight = i + RL[i] -1;
pos = i;
} res += RL[i]/2;
}
return res;
}
};

LeetCode 647. Palindromic Substrings的三种解法的更多相关文章

  1. [LeetCode] 647. Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  2. Leetcode 647. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  3. LeetCode算法题-Move Zeroes(Java实现-三种解法)

    这是悦乐书的第201次更新,第211篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第67题(顺位题号是283).给定一个数组nums,写一个函数将所有0移动到它的末尾,同 ...

  4. LeetCode算法题-First Bad Version(Java实现-三种解法)

    这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最 ...

  5. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  6. 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...

  7. [LeetCode] Remove Element (三种解法)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  8. 【Leetcode】647. Palindromic Substrings

    Description Given a string, your task is to count how many palindromic substrings in this string. Th ...

  9. 647. Palindromic Substrings 互文的子字符串

    [抄题]: Given a string, your task is to count how many palindromic substrings in this string. The subs ...

随机推荐

  1. Python Ethical Hacking - Malware Packaging(2)

    PACKAGING FOR WINDOWS FROM LINUX For best results package the program from the same OS as the target ...

  2. NPOI Excel设置样式

    在表格导出时,会碰到样式修改的问题,作如下简单归纳: //创建行样式ICellStyle style = workbook.CreateCellStyle();//前景色                ...

  3. 题解 CF585F 【Digits of Number Pi】

    考虑用数位 \(DP\) 来统计数字串个数,用 \(SAM\) 来实现子串的匹配. 设状态 \(f(pos,cur,lenth,lim,flag)\),表示数位的位数,在 \(SAM\) 上的节点,匹 ...

  4. tomcat 认证爆破之custom iterator使用

    众所周知,BurpSuite是渗透测试最基本的工具,也可是神器,该神器有非常之多的模块:反正,每次翻看大佬们使用其的骚操作感到惊叹,这次我用其爆破模块的迭代器模式来练练手[不喜勿喷] 借助vulhub ...

  5. DNA Consensus String UVA - 1368

    题目链接:https://vjudge.net/problem/UVA-1368 题意:给出一组字符串,求出一组串,使与其他不同的点的和最小 题解:这个题就是一个点一个点求,利用桶排序,求出最多点数目 ...

  6. php 导出数据到excel类

    原文链接地址:http://www.oschina.net/code/snippet_212240_21885 标注:在使用时一定要屏蔽掉//$bodyVal = $this->charset( ...

  7. MacOS下Lucene学习

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"lucene"获取视频和教程资料! b站在线视频 全文 ...

  8. springboot 使用 dev tool 导致 CastException

    1.背景 项目使用了 Spring + shiro 实现 权限控制, 使用AOP 对 每个 Controller 进行 log 记录时,需要从 shiro 中 获取 username字段, 问题就这样 ...

  9. PHP 函数实例讲解

    PHP 函数 PHP 的真正威力源自于它的函数. 在 PHP 中,提供了超过 1000 个内建的函数. PHP 内建函数 如需查看所有数组函数的完整参考手册和实例,请访问我们的 PHP 参考手册. P ...

  10. luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树

    LINK:字符串树 先说比较简单的正解.由于我没有从最简单的考虑答案的角度思考 所以... 下次还需要把所有角度都考察到. 求x~y的答案 考虑 求x~根+y~根-2*lca~根的答案. 那么问题变成 ...