题目链接:

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的更多相关文章

  1. LN : leetcode 730 Count Different Palindromic Subsequences

    lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a ...

  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. SWFUpload多文件上传使用指南

    SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大.以前在项目中用过几次,但它的配置参数太多了,用过后就忘记怎么用了,到以后要用时又得到官网上看它的文档,真是太烦了.所以 ...

  2. Nginx禁止目录执行php文件权限

    location ~ /dir/.*.(php|php5)?$ { deny all; } 禁止dir目录执行php文件权限 .csharpcode, .csharpcode pre { font-s ...

  3. Luogu P4707 重返现世

    题目描述 为了打开返回现世的大门,Yopilla 需要制作开启大门的钥匙.Yopilla 所在的迷失大陆有 \(n\) 种原料,只需要集齐任意 \(k\) 种,就可以开始制作. Yopilla 来到了 ...

  4. swift面向协议编程

    https://www.technotification.com/2018/08/protocol-oriented-programming-swift.html https://www.toptal ...

  5. 3D数学读书笔记——矩阵基础

     本系列文章由birdlove1987编写,转载请注明出处.    文章链接:http://blog.csdn.net/zhurui_idea/article/details/24975031   矩 ...

  6. [NOI2003],[AHOI2006]文本编辑器

    嘟嘟嘟 [NOI2003]的其实就是一个板子--所以我就不说啥了. 唯一需要注意的是读入字符(哎--):题中说"中间可能有空格,请忽略"的意思是要在程序里特判掉,不是不管他-- 输 ...

  7. 30个你 “ 不可能全部会做 ” 的javascript题目

    1,以下表达式的运行结果是: ["1","2","3"].map(parseInt) A.["1","2&qu ...

  8. laravel框架入门

    本文摘自网络,个人感觉写的很不错,决定收藏一下纯属本人学习之用 本文介绍如何开始使用 Laravel. 读完本文,你将学到: 如何安装 Laravel,新建 Laravel 程序,如何连接数据库: L ...

  9. VD: error VERR_FILE_NOT_FOUND

    virtualbox制作的镜像文件如果移动了位置,比如从C盘移到D盘,那么再次打开时会提示找不到文件. 解决办法: 打开virtualbox,在“管理”菜单中打开“虚拟介质管理”,在“虚拟硬盘”选项卡 ...

  10. CF293B Distinct Paths 搜索

    传送门 首先数据范围很假 当\(N + M - 1 > K\)的时候就无解 所以对于所有要计算的情况,\(N + M \leq 11\) 超级小是吧,考虑搜索 对于每一个格子试填一个数 对于任意 ...