lc 730 Count Different Palindromic Subsequences


730 Count Different Palindromic Subsequences

Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.

A subsequence of a string S is obtained by deleting 0 or more characters from S.

A sequence is palindromic if it is equal to the sequence reversed.

Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_i.

Example 1:

Input:
S = 'bccb'
Output: 6
Explanation:
The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
Note that 'bcb' is counted only once, even though it occurs twice.

Example 2:

Input:
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output: 104860361
Explanation:
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.

Note:

The length of S will be in the range [1, 1000].

Each character S[i] will be in the set {'a', 'b', 'c', 'd'}.

带记忆数组 Accepted

虽然题目只要求四个字母,但我们扩展普遍性,这里就做二十六个字母的。带记忆数组和动态规划的本质是差不多的。带记忆数组memo的递归解法,这种解法的思路是一层一层剥洋葱,比如"bccb",按照字母来剥,先剥字母b,确定最外层"b _ _ b",这会产生两个回文子序列"b"和"bb",然后递归进中间的部分,把中间的回文子序列个数算出来加到结果res中,然后开始剥字母c,找到最外层"cc",此时会产生两个回文子序列"c"和"cc",然后由于中间没有字符串了,所以递归返回0,按照这种方法就可以算出所有的回文子序列了。

class Solution {
public:
int countPalindromicSubsequences(string S) {
int len = S.size();
vector<vector<int>> dp(len+1, vector<int>(len+1, 0));
vector<vector<int>> ch(26, vector<int>());
for (int i = 0; i < len; i++) {
ch[S[i]-'a'].push_back(i);
}
return calc(S, ch, dp, 0, len);
}
int calc(string S, vector<vector<int>>& ch, vector<vector<int>>& dp, int start, int end) {
if (start >= end) return 0;
if (dp[start][end] > 0) return dp[start][end];
long ans = 0;
for (int i = 0; i < 26; i++) {
if (ch[i].empty()) continue;
auto new_start = lower_bound(ch[i].begin(), ch[i].end(), start);
auto new_end = lower_bound(ch[i].begin(), ch[i].end(), end) - 1;
if (new_start == ch[i].end() || *new_start >= end) continue;
ans++;
if (new_start != new_end) ans++;
ans += calc(S, ch, dp, *new_start+1, *new_end);
}
dp[start][end] = ans % int(1e9+7);
return dp[start][end];
}
};

LN : leetcode 730 Count Different Palindromic Subsequences的更多相关文章

  1. leetcode 730 Count Different Palindromic Subsequences

    题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...

  2. [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  3. 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...

  4. 730. Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  5. [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  6. [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  7. Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  8. 乘风破浪:LeetCode真题_005_Longest Palindromic Substring

    乘风破浪:LeetCode真题_005_Longest Palindromic Substring 一.前言 前面我们已经提到过了一些解题方法,比如递推,逻辑推理,递归等等,其实这些都可以用到动态规划 ...

  9. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

随机推荐

  1. HDU 5438 Ponds

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  2. 蓝牙协议(bluetooth spec)

    1.概述:   蓝牙协议规范遵循开放系统互连参考模型(OSI/RM),从低到高地定义了蓝牙协议堆栈的各个层次. SIG(Session Initiation Protocol)所定义的蓝牙技术规范的目 ...

  3. html5--6-55 动画效果-关键帧动画

    html5--6-55 动画效果-关键帧动画 实例 @charset="UTF-8"; div{ width: 150px; height: 150px; font-size: 2 ...

  4. trying to draw too large(106,975,232 bytes) bitmap.

    Loading Large Bitmaps Efficiently This lesson teaches you to Read Bitmap Dimensions and Type Load a ...

  5. CodeForces937B:Vile Grasshoppers(素数性质)

    The weather is fine today and hence it's high time to climb the nearby pine and enjoy the landscape. ...

  6. BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序

    BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序 Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息 ...

  7. Transformations

    链接 分析:根据操作模拟 /* ID:wanghan PROB:transform LANG:C++ */ #include "iostream" #include "c ...

  8. AtCoder Beginner Contest 057

    A题 分析:24进制转换 #include<iostream> using namespace std; int main() { int a,b; while(cin>>a& ...

  9. 当把链接保存到手机桌面。设置图标 只在safari浏览器中有用

    <link rel="apple-touch-icon" sizes="114x114" href="images/logo.png" ...

  10. 初识NDA

    领导要签署保密协议,全是英文,叫我来翻译一下,这样便接触到了NDA,翻译过程中遇到不少问题,既然接触到了,还是多查查资料了解一下,以备以后的不时之需. NDA 全称 Non Disclosure Ag ...