当前位置:网站首页>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
边栏推荐
- Summary of DOM knowledge points
- 05 classification learning notes lihongyi's in-depth study 2021
- SAP SD 创建/修改价格表
- Meet o & M (I) common questions for O & M interview
- How to achieve fair and equitable data access and service ecology?
- Binary sort tree
- [day 7 of JUC learning] reentrantlock and reentrantreadwritelock
- (linear DP | monotone queue) acwing 895 Longest ascending subsequence
- Les produits antigéniques entrent dans la famille et les entreprises chinoises de dispositifs médicaux accueillent un nouvel océan bleu
- 【juc学习之路第6天】并发计算器与线程随机因子
猜你喜欢

Anaconda download package error: valueerror: check_ hostname requires server_ hostname

2022 618 notebook shopping guide

Flex flexible layout tutorial and understanding of the main axis cross axis: Grammar

mysql5和mysql8同时安装

Oracle uses Navicat tool to import and export data

The road of global evolution of vivo global mall -- multilingual solution

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

Class. Getresource() and class Getresourceasstream() method
![Divide the array into three equal parts [problem analysis]](/img/0b/856fcceb0373baa8acb46e9ae2c861.png)
Divide the array into three equal parts [problem analysis]

How many steps does it take for C language to become Fibonacci number
随机推荐
Test case design method
Wechat applet Bluetooth development
sonarqube介绍和安装步骤
二叉排序树
oracle使用Navicat工具导入导出数据
Antigen products enter the family, and Chinese medical device enterprises usher in a new blue ocean
Acwing's first question solution attracted the first fan!!! Happy~~~
(dp+ group backpack) acwing 9 Group knapsack problem
【JUC系列】Executor框架之概览
2022 618笔记本选购指北
QR code
Notes on knowledge points of dynamic planning
Ar helps brand stores achieve global data growth
swiper
【BBC learningenglish】with Tango
10. logical statement
抗原产品进入家庭,中国医疗器械企业迎来新蓝海
Procédures d'introduction et d'installation de sonarqube
Teach you to play with SSM framework
Oracle uses Navicat tool to import and export data