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];
};
转载于:https://www.cnblogs.com/fenice/p/7979770.html
leetcode 730 Count Different Palindromic Subsequences相关推荐
- 【Hard 递归 动态规划 回文串15】LeetCode 730. Count Different Palindromic Subsequences
LeetCode 730. Count Different Palindromic Subsequences 博客转载自:http://zxi.mytechroad.com/blog/dynamic- ...
- Lintcode 738.Count Different Palindromic Subsequences go
/** 738 · 计数回文子序列 算法 Hard Accepted Rate 34% DescriptionSolutionNotesDiscussLeaderboard Description 给 ...
- LeetCode算法入门- Longest Palindromic Substring-day5
LeetCode算法入门- Longest Palindromic Substring-day5 Longest Palindromic Substring Given a string s, fin ...
- LeetCode 38. Count and Say
问题链接 LeetCode 38. Count and Say 题目解析 找规律,每一个数字串是上一个数字串的"读法".比如:n=1时为"1",读作" ...
- [勇者闯LeetCode] 38. Count and Say
[勇者闯LeetCode] 38. Count and Say Description The count-and-say sequence is the sequence of integers b ...
- LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题
题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...
- LeetCode 250. Count Univalue Subtrees
原题链接在这里:https://leetcode.com/problems/count-univalue-subtrees/ 题目: Given a binary tree, count the nu ...
- leetcode -- 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- LeetCode 1885. Count Pairs in Two Arrays(二分查找)
文章目录 1. 题目 2. 解题 1. 题目 Given two integer arrays nums1 and nums2 of length n, count the pairs of indi ...
- leetcode 1925. Count Square Sum Triples(python)
描述 A square triple (a,b,c) is a triple where a, b, and c are integers and a^2 + b^2 = c^2. Given an ...
最新文章
- [代码]--WinForm 窗体之间相互嵌套
- linux 命令 读phy_CentOS教程(七)- 常用命令使用介绍(上)
- 算法--------俄罗斯套娃信封问题(Java版本)
- Silverlight4 ColorPicker控件
- Unity3D导出的EXE不用显示分辨率选择界面
- window.onerror=hide_error_message;
- GEO学习笔记-P1-P2
- PC 上安装Windows10系统到硬盘上ESP分区丢失,新建ESP分区修复引导ESP分区创建失败解决办法
- Prometheus监控学习笔记之Prometheus普罗米修斯监控入门
- [Scala的协变和逆变]
- 我们说运营,到底是在说运营什么?
- 机器学习之监督学习:分类
- Linux Snap 命令
- 总账凭证之间核销的SQL
- MongoDB 存储图片
- 天时地利人和—一个传奇操作系统的诞生记
- 用虚拟打印机MacroMedia FlashPaper 制作swf文件
- 昨天又让3位读者挣了200,满足
- 你管这叫操作系统源码(九)
- 史前文化:中华文化之根
热门文章
- 软件基本功:出错了就问别人,大哥你是不是开发人员?
- 编译imsdroid,折腾了半天,还是弃用了Android Studio,换用Eclipse
- 大庆油田真正解决了吃饭问题
- C++模板实现,支持多维,安全数组的完整代码
- 简单的Python文件服务器和HTTP POST上传文件C代码
- A 1049 Counting Ones (30分)
- spark中local模式与cluster模式使用场景_详细总结spark基于standalone、yarn集群提交作业流程...
- cmd 看图片十六进制_Fun Python | 女朋友让我把这网站上的图片都下载下来
- ajax可以获取作用域的值吗,javascript – 为什么ajax调用中的闭包可以访问外部作用域?...
- Abbirb120型工业机器人_你知道机器人有几种编程方式吗?