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. (C)do{...}while(0);的用法及意义

    实际上,do{…}while(0)的作用远大于美化你的代码. 总结起来这样写主要有以下几点好处: 1. 辅助定义复杂的宏 避免引用的时候出错: 举例来说,假设你需要定义这样一个宏: #define D ...

  2. js中一些常见写法的含义

    1. 常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号 ...

  3. 织梦CMS如何在首页调用指定的文章 idlist

    在网站首页调用站内新闻是必不可少的,但是有的时候不能根据自己的需要来调用指定的文章,想要调用自己指定的文章还要做一些修改. 在网站中调用指定文章可以使用织梦默认的标签idlist,在调用的时候使用以下 ...

  4. codeforces 460A Vasya and Socks 解题报告

    题目链接:http://codeforces.com/problemset/problem/460/A 题目意思:有一个人有 n 对袜子,每天早上会穿一对,然后当天的晚上就会扔掉,不过他会在 m 的倍 ...

  5. html5--3.1 form元素

    html5--3.1 form元素 学习要点 form元素及其属性 form元素 用来定义一个表单,是建立表单的基础元素,(就类似定义表格的table) 表单的其他元素包含在form元素中,其主要子元 ...

  6. macbook pro上安装虚拟机

    第一步:下载MacHunter的app应用商店 第二步:在MacHunter内下载Parallels Desktop虚拟机 第三步:如果在这个商店下载不下来,在网络资源上直接下载Parallels D ...

  7. E20180425-hm

    zoom n. 变焦; 嗡嗡声; 隆隆声; (车辆等) 疾驰的声音; deprecate  vt. 不赞成,不推荐, 反对;

  8. E20180413-hm

    skew adj. 斜的,歪的; [数学] 不对称的; [统计学] 歪斜,扭曲;   vt. 歪曲; 曲解; 使歪斜;   vi. 偏离,歪斜; 斜视; traversal  n. 横越,横断物,(横 ...

  9. Ruby基本语法

    更新: 2017/06/09 更新: 2017/06/20 cattr_accessor定义类变量,相当于@@ 更新: 2017/06/23 生成类的实例 更新: 2017/06/24 补充loop的 ...

  10. 【WIP】swift3的timer的用法

    创建: 2017/10/14   更新: 2017/10/14 标题加上[WIP],补充创建时间     回家再写