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 ...
随机推荐
- ios 百度地图使用
第一步.引入 1.下载SDK 地址http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download 2.解压出Baid ...
- Shiro-Subject 分析
Subject反正就好像呈现的视图.所有Subject 都绑定到SecurityManager,与Subject的所有交互都会委托给SecurityManager:可以把Subject认为是一个门面: ...
- golang的reflection(转)
作者:BGbiao 链接:https://www.jianshu.com/p/42c19f88df6c 來源:简书 反射reflection 可以大大提高程序的灵活性,使得interface{}有更大 ...
- Appium1.9.1 之 Desired Capabilities 释疑
服务关键字 Desired Capabilities在启动session的时候是必须提供的. Desired Capabilities本质上是以key value字典的方式存放,客户端将这些键值对发给 ...
- <20180930>故障解决记录Cisco RV系列低端路由
故障描述: 今日网络瘫痪 , 浏览器提示报 DNS出错, 故障现象: 网关能正常分配到IP但内网机器却无法上网. 其中一个WiFi设备竟然还可以通讯(聊天工具)! 故障思路和排查: 1.1 怀疑是上次 ...
- day14 Python集合关系运算交,差,并集
low逼写法,没用集合 python_1 = ['charon','pluto','ran'] linux_1 = ['ran','xuexue','ting'] python_and_linux = ...
- Linux 性能分析工具汇总合集
出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章.本文也可以作为检验基础知识的指标,另外文章涵盖了一个系统的方方面面.如果没有完善的计算机系统知识,网络知识和操作系统知识, ...
- 201904:Action recognition based on 2D skeletons extracted from RGB videos
论文标题:Action recognition based on 2D skeletons extracted from RGB videos 发表时间:02 April 2019 解决问题/主要思想 ...
- 工具 使用Fiddler进行手机抓包
Fiddler 手机抓包 Web代理服务器 可以抓https包 手机和电脑处于同一网络 Tools -> Options... -> Connections Allow remote co ...
- Linux 读取 (*.xls)文件读取,使用libxls库
首先下载libxls,项目地址:http://sourceforge.net/projects/libxls/,备用下载:libxls-1.4.0.zip 解压后使用: ./configure --p ...