您的位置:首页 > 其它

【算法分析与设计】【第十四周】647. Palindromic Substrings

2017-12-05 11:27 232 查看
题目来源:674 https://leetcode.com/problems/palindromic-substrings/description/

Palindromic Substrings
题目大意

思路
相关回顾最长回文子串

相关回顾最长回文子序列

本题

解题代码

复杂度

动态规划基础训练。

647. Palindromic Substrings

题目大意

给定一个字符串,求出该字符串包含的所有回文子串的个数。

Example 1:

Input: “abc”

Output: 3

Explanation: Three palindromic strings: “a”, “b”, “c”.

Example 2:

Input: “aaa”

Output: 6

Explanation: Six palindromic strings: “a”, “a”, “a”, “aa”, “aa”, “aaa”.

思路

解这题之前先来复习一下两道相关的题:求
最长回文子串
最长回文子序列


先区别一下这两个概念:子串和子序列。

子串:XiXi+1Xi+2…Xj-1Xj,为原串的某一连续部分。

子序列:可以为原串的某一不连续部分。

例:

原串:todayisasunnyday

子串:isasunn

子序列:odand

相关回顾:最长回文子串

最长公共子串
本质一致,只需将原串作为X串,原串逆过来作为Y串,进行比较就可以了。

状态转移式:

// x为原字符串,y为原字符串逆置得到的串,f[i][j]表示从x[i]到y[j]之间的最长回文子序列的长度

init:f[i][i] = 1

f[i][j] = f[i-1][j-1]       ,x[i]=y[j],i!=j
f[i][j] = 0                 ,x[i]!=y[j],i!=j

result:max{f[i][j]}


相关回顾:最长回文子序列

《算法概论》P179的习题Ex6.7

状态转移式:

// str为原字符串,f[i][j]表示从str[i]到str[j]之间的最长回文子序列的长度

init:f[i][i] = 1

f[i][j] = f[i+1][j-1] + 2               ,str[i]=str[j],i!=j
f[i][j
4000
] = max{f[i+1][j], f[i][j-1]}     ,str[i]!=str[j],i!=j

result:max{f[i][j]}


本题

状态转移式:

// str为原字符串,(bool)isPal[i][j]表示从str[i]到str[j]是否是回文

init:isPal[i][i] = true

isPal[i][j] = f[i+1][j-1]   ,str[i]=str[j],i!=j,i+1<j-1
isPal[i][j] = true          ,str[i]=str[j],i!=j,i+1>j-1


解题代码

class Solution {
public:
int countSubstrings(string s) {
int size = s.length();
if (size <= 1) return 1;
bool isPal[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
isPal[i][j] = false;
}
}
for (int i = 0; i < size; i++) isPal[i][i] = true;
int count = size;
for (int j = 0; j < size; j++) {
for (int i = 0; i < j; i++) {
if (s[i] == s[j] && (isPal[i+1][j-1] || i+1 >= j-1) ) {
isPal[i][j] = true;
count++;
}
}
}
return count;
}
};


复杂度

O(n^2)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: