leetcode 730 Count Different Palindromic Subsequences
题目链接:
https://leetcode.com/problems/count-different-palindromic-subsequences/description/
730.Count Different Palindromic Subsequences
题意
给你一个只包含a、b、c、d字符的字符串,总共有多少个不重复的回文子序列。
题解
容易想到这题可以用动态规划的方法来解决。
1、dp[l][r][k]表示区间[l,r]内,以字符k+'a'结尾的回文子序列个数。
当k+'a'S[l]S[r]的时候,我们考虑两种情况:
1)、l,r不加进来:dp[l+1][r-1][k],k'属于[0,3];
2)、l,r加进来:sigma(dp[l+1][r-1][k'])+2,其中0<=k'<=3。
由于要考虑不重复,经过观察容易发现第1)种情况恰好会被第2)种情况所包含,所以我们可以得出最终结论:dp[l][r][k]=sigma(dp[l+1][r-][k']+2),0<=k'<=3。(具体的转移代码中体现)
const int mod=1e9+7;
class Solution {
public:
int dfs(int l,int r,int k,string& S,vector<vector<vector<int> > >& dp) {
if(r<l) return 0;
if(l==r) return (k==S[l]-'a')?1:0;
if(dp[l][r][k]>=0) return dp[l][r][k];
int& res=dp[l][r][k]=0;
if(r-l==1) {
if(S[l]==S[r]&&k==S[l]-'a') return res=2;
if(k==S[l]-'a'||k==S[r]-'a') return res=1;
return res=0;
}
if(S[l]==S[r]&&S[l]-'a'==k) {
res=2;
for(int i=0; i<4; i++) {
res+=dfs(l+1,r-1,i,S,dp);
res%=mod;
}
} else {
if(S[l]-'a'==k){
res=dfs(l,r-1,k,S,dp);
}else{
res=dfs(l+1,r,k,S,dp);
}
}
return res;
}
int countPalindromicSubsequences(string S) {
int n=S.length();
vector<vector<vector<int> > > dp(n,vector<vector<int> >(n,vector<int>(4,-1)));
int ans=0;
for(int i=0; i<4; i++) {
ans+=dfs(0,n-1,i,S,dp);
ans%=mod;
}
return ans;
}
};
2、dp[l][r]表示子串[l,r]中的不重复回文子序列,则容易得到转移方程dp[l][r]=sigma(dp[l[k']+1][r[k']-1]+l[k']==r[k']?1:2),其中0<=k'<=3。并且l[k']代表从l(包括l自己)往右第一个为k'+'a'的字符,r[k']代表从r(包括r自己)往左第一个为k'+'a'的字符。
const int mod=1e9+7;
typedef long long LL;
class Solution {
public:
int dfs(int l,int r,string& S,vector<vector<LL> >& dp) {
if(l>r) return 0;
if(dp[l][r]>=0) return dp[l][r];
LL& res=dp[l][r]=0;
for(int i=0;i<4;i++){
int lef=nxt[l][i],rig=pre[r][i];
if(lef>rig) continue;
if(S[lef]==S[rig]){
res++;
if(lef<rig) res++;
}
res+=dfs(lef+1,rig-1,S,dp);
res%=mod;
}
return res;
}
void init(int n,string& S){
int pos[4];
memset(pos,-1,sizeof(pos));
for(int i=0;i<n;i++){
pos[S[i]-'a']=i;
for(int j=0;j<4;j++){
pre[i][j]=pos[j];
}
}
for(int i=0;i<4;i++) pos[i]=n;
for(int i=n-1;i>=0;i--){
pos[S[i]-'a']=i;
for(int j=0;j<4;j++){
nxt[i][j]=pos[j];
}
}
}
int countPalindromicSubsequences(string S) {
int n=S.length();
init(n,S);
vector<vector<LL> > dp(n,vector<LL>(n,-1));
return dfs(0,n-1,S,dp);
}
private:
int nxt[1001][4],pre[1001][4];
};
leetcode 730 Count Different Palindromic Subsequences的更多相关文章
- LN : leetcode 730 Count Different Palindromic Subsequences
lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a ...
- [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 ...
随机推荐
- 17秋 软件工程 团队第五次作业 Alpha Scrum4
17秋 软件工程 团队第五次作业 Alpha Scrum4 今日完成的任务 世强:部门基础信息模块数据更新.部门审核提交: 港晨:设计编写登录界面的一部分: 树民:学习python基本语法.flask ...
- 阿里八八β阶段Scrum(5/5)
今日进度 陈裕鹏: 简单信息抽取编码完成 叶文滔: 处理了信息抽取编码的一些BUG,修复了日程界面不会自动更新添加的日程的BUG,修改了原先测试用的TAG以及数据分析部分数据计算数值错误的问题 王国超 ...
- Sketch网页截屏插件设计开发
1.需求 在Sketch的Artboard中插入网页截图: 1.1.输入网址,自动截图到Artboard中,并居中显示: 1.2.可截取网页局部图片 2.技术选型 技术的选型主要是针对截图功能的选型, ...
- ROS教程2 编写简单的消息发布器和订阅器 (C++ catkin)
创建工作环境 创建文件夹,创建包 mkdir -p ~/catkin_arduino_ros/src catkin_create_pkg test1_pub_sub std_msgs rospy ro ...
- kumavis/obj-multiplex
https://github.com/kumavis/obj-multiplex obj-multiplex多路复用 simple stream multiplexing for objectMode ...
- sublime text3 下配置python3!
1.下载sublime3和python3(在腾讯软件中心下载较快) 2.安装 3.打开sublime ,tools->build system->new build system,在文件中 ...
- Objective-C Mach-O文件格式深入理解
Mach-O(Mach Object),是一种基于Mach内核的文件格式,苹果很多文件都采用这种格式,最常见的就是可执行文件和动态库. 当然,还有.o的目标文件..a和.framework的静态库以及 ...
- MySQL 基础七 视图
-- 查看表 SELECT * FROM student; SHOW CREATE TABLE student; -- 创建视图 CREATE VIEW v_student1 AS SELECT *F ...
- Photoshop 基础一 安装
安装 版本介绍 学习教程 一.安装 1)注册Adobe账号,注册地址:Adobe注册 2)下载地址:Adobe下载 下载地址2:百度经验 3)安装:试用期7天的版本 二.版本介绍 1)最新版本:A ...
- C语言程序设计II—第九周教学
第九周教学总结(22/4-28/4) 教学内容 本周的教学内容为: 9.1 输出平均分最高的学生信息 知识点:结构的概念.结构的定义形式.结构的嵌套定义.结构变量和结构成员变量的引用.重难点:结构变量 ...