当前位置:网站首页>[730. statistics of different palindrome subsequences]
[730. statistics of different palindrome subsequences]
2022-06-10 10:10:00 【Sugar_ wolf】
source : Power button (LeetCode)
describe :
Given a string s, return s Different non empty in 「 Palindrome subsequence 」 Number .
By getting from s Delete in 0 One or more characters to obtain subsequences .
If a character sequence is consistent with the character sequence it reverses , So it's 「 Palindrome character sequence 」.
If there is one i , Satisfy ai != bi , Then two sequences a1, a2, ... and b1, b2, ... Different .
Be careful :
- The results could be big , You need to 109 + 7 modulus .
Example 1:
Input :s = 'bccb'
Output :6
explain :6 Different sequences of non empty palindromes are :'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
Be careful :'bcb' It appears twice but only counts once .
Example 2:
Input :s = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output :104860361
explain : share 3104860382 Different non empty palindrome subsequences ,104860361 Yes 109 + 7 The value after taking the mold .
Tips :
1 <= s.length <= 1000s[i] Contains only 'a', 'b', 'c' or 'd'
Method 1 : Dynamic programming ( Using 3D arrays )
Ideas and algorithms



Code :
class Solution {
public:
const int MOD = 1e9 + 7;
int countPalindromicSubsequences(string &s) {
int n = s.size();
vector<vector<vector<int>>> dp(4, vector<vector<int>>(n, vector<int>(n, 0)));
for (int i = 0; i < n; i++) {
dp[s[i] - 'a'][i][i] = 1;
}
for (int len = 2; len <= n; len++) {
for (int i = 0, j = len - 1; j < n; i++, j++) {
for (char c = 'a', k = 0; c <= 'd'; c++, k++) {
if (s[i] == c && s[j] == c) {
dp[k][i][j] = (2LL + dp[0][i + 1][j - 1] + dp[1][i + 1][j - 1] + dp[2][i + 1][j - 1] + dp[3][i + 1][j - 1]) % MOD;
} else if (s[i] == c) {
dp[k][i][j] = dp[k][i][j - 1];
} else if (s[j] == c) {
dp[k][i][j] = dp[k][i + 1][j];
} else {
dp[k][i][j] = dp[k][i + 1][j - 1];
}
}
}
}
int res = 0;
for (int i = 0; i < 4; i++) {
res = (res + dp[i][0][n - 1]) % MOD;
}
return res;
}
};
Execution time :260 ms, In all C++ Defeated in submission 45.32% Users of
Memory consumption :153.2 MB, In all C++ Defeated in submission 10.84% Users of
Method 2 : Dynamic programming ( Use a two-dimensional array )
Ideas and algorithms



Code :
class Solution {
public:
const int MOD = 1e9 + 7;
int countPalindromicSubsequences(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n));
for (int i = 0; i < n; i++) {
dp[i][i] = 1;
}
for (int len = 2; len <= n; len++) {
for (int i = 0; i + len <= n; i++) {
int j = i + len - 1;
if (s[i] == s[j]) {
int low = i + 1;
int high = j - 1;
while (low <= high && s[low] != s[i]) {
low++;
}
while (high >= low && s[high] != s[j]) {
high--;
}
if (low > high) {
dp[i][j] = (2 + dp[i + 1][j - 1] * 2) % MOD;
} else if (low == high) {
dp[i][j] = (1 + dp[i + 1][j - 1] * 2) % MOD;
} else {
dp[i][j] = (0LL + dp[i + 1][j - 1] * 2 - dp[low + 1][high - 1] + MOD) % MOD;
}
} else {
dp[i][j] = (0LL + dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + MOD) % MOD;
}
}
}
return dp[0][n - 1];
}
};
Execution time :96 ms, In all C++ Defeated in submission 95.07% Users of
Memory consumption :35.6 MB, In all C++ Defeated in submission 58.62% Users of
Complexity analysis
Time complexity :O(n2), among n For the string s The length of . The time complexity mainly depends on the number of States to be solved .
Spatial complexity :O(n2), among n For the string s The length of . The spatial complexity mainly depends on the total number of states in the dynamic programming model .
author:LeetCode-Solution
边栏推荐
- 九、委托模式
- 【并发编程JUC】创建线程的四种方式
- Google Earth Engine(GEE)——GPWv411:平均行政单位面积数据集
- 单片机开发需要的工具以及软件有哪些
- Uncaught TypeError: Cannot read properties of undefined (reading ‘colspan‘)
- mdb转换为db文件
- 【边缘检测】基于matlab八方向sobel图像边缘检测【含Matlab源码 1865期】
- R语言plotly可视化:plotly可视化箱图、使用quartilemethod参数自定义设置箱图分位数的计算方法(Basic Boxplot)
- osg基本操作
- 2022年普通脚手架工(建筑特殊工种)操作证考试题库及模拟考试
猜你喜欢

Theory and application of image processing

致广大、尽精微,曙光问道算力服务“神经系统”

成都自控开发_单片机程序的一般开发流程是怎样

“胡说八道” DATABASE 主键设计

Product recommendation system based on deep learning (WEB)

收藏 | VLOOKUP函数的这些妙用你都知道吗?

一个独特的简历生成器,开源了!

【730. 统计不同回文子序列】

Fastadmin uses phpexcel to export tabular data to excel

Why is your next computer a computer? Explore different remote operations
随机推荐
Google Earth Engine(GEE)——GPWv411:平均行政单位面积数据集
成都測試設備定制_單片機C語言之數據類型初步介紹
协程asyncio异步编程
Chengdu test equipment customization_ A preliminary introduction to the data types of C language of single chip microcomputer
Phpstrom télécharge le projet dans le cloud de code
Product recommendation system based on deep learning (WEB)
How to handle art record? What materials should be prepared for handling the art record?
Example 4 of lambda expression
OSG basic operation
Lambda表达式例二
微软再曝“丑闻”:在办公室看 VR 黄片,“HoloLens 之父”即将离职!
2022年煤矿井下电气考试题库及在线模拟考试
力扣 1037. 有效的回旋镖
fastadmin使用PHPExcel导出表格数据到Excel中
张小白教你使用OGG实现Oracle 19C到MySQL 5.7的数据同步(3)
成都自控开发_单片机程序的一般开发流程是怎样
2022年金属非金属矿山提升机操作考试题库及答案
MDB to DB file
Fastadmin uses phpexcel to export tabular data to excel
2022年普通脚手架工(建筑特殊工种)操作证考试题库及模拟考试
