当前位置:网站首页>730.Count Different Palindromic Subsequences
730.Count Different Palindromic Subsequences
2022-06-11 23:59:00 【SUNNY_ CHANGQI】
The description of the porblem
Given a string s, return the number of different non-empty palindromic subsequences in s.
Since the answer may be very large, return it modulo 1e9+7
A sequence is palindromic if it is equal to the sequence reversed.
Two sequences a1, a2. ⋯ \cdots ⋯ and b1, b2, ⋯ \cdots ⋯ are different if there is some i i i for which ai ! = != != bi.
example
Input: s = "bccb"
Output: 6
Explanation: The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
Note that 'bcb' is counted only once, even though it occurs twice.
source : Power button (LeetCode)
link :https://leetcode.cn/problems/count-different-palindromic-subsequences
The intuition (dynamic programming)
The codes;
#include <string>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int countPalindromicSubsequences(string s) {
const int MODE = 1e9 + 7;
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 < 'e'; ++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]) % MODE;
} else if (s[i] == c && s[j] != c) {
dp[k][i][j] = dp[k][i][j-1];
} else if (s[i] != c && 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 += dp[i][0][n-1];
res %= MODE;
}
return res;
}
};
int main() {
Solution s;
//"bcbacbabdcbcbdcbddcaaccdcbbcdbcabbcdddadaadddbdbbbdacbabaabdddcaccccdccdbabcddbdcccabccbbcdbcdbdaada"
string s1 = "bcbacbabdcbcbdcbddcaaccdcbbcdbcabbcdddadaadddbdbbbdacbabaabdddcaccccdccdbabcddbdcccabccbbcdbcdbdaada";
cout << "res: " << s.countPalindromicSubsequences(s1) << endl;
return 0;
}
#The results
$ ./test
res: 823023321
边栏推荐
- JS to add an attribute to an array object
- New Year Countdown JS case
- My creation anniversary
- Top selling commodities 【 project mall 】
- (interval DP | dfs+ memory) acwing 282 Stone merging
- Jenkins of the integrate tool
- Mmdetection custom fetch detection result script and image_ demo. Py parsing
- What is webstorage? And cookies
- [pat (basic level) practice] - [simple simulation] 1076 WiFi password
- Mathematical modeling experience ----- summary of three modeling
猜你喜欢

CD process

(dp) acwing 899. Edit distance

How many steps does it take for C language to become Fibonacci number

Cisco private dynamic routing protocol: EIGRP

Teach you to play with SSM framework

明德扬ADC系列开发板-Ad9653子板 多通道 高分辨率 高采样率

自定义JSP标签->概念->生命周期

(greedy + longest ascending subsequence) acwing 896 Longest ascending subsequence II

(dp+ longest common subsequence) acwing 897 Longest common subsequence
![[signals and systems] (XXII) Laplace transform and complex frequency domain analysis - s-domain analysis](/img/35/a23646ac8501ac646cd44eeecd5b79.jpg)
[signals and systems] (XXII) Laplace transform and complex frequency domain analysis - s-domain analysis
随机推荐
JS——防止自动恢复页面位置
Display product details [project mall]
Mingdeyang FPGA development board xilinx-k7 core board kinex7k325 410t industrial grade
MySQL some simple commands
swiper
【juc学习之路第5天】引用原子类和属性修改器
Lake Shore - supertran continuous flow cryogenic thermostat system
Teach you to play with SSM framework
【BBC learningenglish】with Tango
DPT-FSNET: DUAL-PATH TRANSFORMER BASED FULL-BAND AND SUB-BAND FUSION NETWORK FOR SPEECH ENHANCEMENT
2022 618 notebook shopping guide
抗原产品进入家庭,中国医疗器械企业迎来新蓝海
Balanced binary tree (AVL tree)
Les produits antigéniques entrent dans la famille et les entreprises chinoises de dispositifs médicaux accueillent un nouvel océan bleu
EFCore中数据表的两种配置方式
SAP SD create / modify price list
平衡二叉树(AVL 树)
Detailed explanation of various objects in browser object
(linear DP | monotone queue) acwing 895 Longest ascending subsequence
(digital statistics dp+good) acwing 338 Counting problem