LN : leetcode 730 Count Different Palindromic Subsequences
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的更多相关文章
- leetcode 730 Count Different Palindromic Subsequences
题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...
- [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...
- 730. Count Different Palindromic Subsequences
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- Count Different Palindromic Subsequences
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- 乘风破浪:LeetCode真题_005_Longest Palindromic Substring
乘风破浪:LeetCode真题_005_Longest Palindromic Substring 一.前言 前面我们已经提到过了一些解题方法,比如递推,逻辑推理,递归等等,其实这些都可以用到动态规划 ...
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
随机推荐
- Java中需要了解的点
1.32位jvm.64位区别? 2.
- LoadRunner检查点使用小结
LR中检查点有两种:图片和文字. 常用检查点函数如下: 1)web_find()函数用于从 HTML 页中搜索指定的文本字符串: 2)web_reg_find()函数注册一个请求,以在下一个操作函数( ...
- BestCoder4 1002 Miaomiao's Geometry (hdu 4932) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 题目意思:给出 n 个点你,需要找出最长的线段来覆盖所有的点.这个最长线段需要满足两个条件:(1 ...
- MyEclipse注释配置
MyEclipse注释配置 配置路径 1.1. JAVA 打开MyEclipse,选择Window>Preferences>Java>Code Style>Code ...
- WAS:Thread "server.startup : 1" (00000020) and may be hung异常
有现场server启动时,启动不了,后台报错如下: [// ::: CST] ThreadMonitor W WSVR0605W: Thread ) has been active milliseco ...
- Linux系统的方法论
Linux系统的方法论 https://www.cnblogs.com/youxia/p/LinuxDesktop001.html 阅读目录 特别说明 什么情况下适合玩Linux桌面 Linux桌面系 ...
- UIWindow学习
写在前面 本文内容绝大部分都参考唐巧大神的<iOS开发进阶>,只是结合不是特别长的开发经验加以补充:最后基于UIWindow自定义了一个类似于微信的ActionSheet. UIWindo ...
- AJAX路径问题
如果发AJAX请求,看发送请求消息的路径,如果看到报404的错,而这个 时候看下发送头部的路径,如果看到%20,这个时候有可能就是写路劲的时候,不小心按了一个空格
- Ruby attr_reader , attr_writer, attr_accessor方法
attr_reader方法------读取实例变量 attr_writer方法------改写实例变量 attr_accessor方法-----读写实例变量 class Person attr_rea ...
- 一个坑爹的Swift报错原因分析与解决方案
有时候在实际开发中,完全没有任何问题.但是一到实机测试,就会直接卡机 let count = scoreStorage.count return scoreStorage[Int(arc4rand ...