当前位置:网站首页>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
边栏推荐
- Jenkins of the integrate tool
- Custom JSP tag - > concept - > lifecycle
- EFCore中数据表的两种配置方式
- Mmdetection custom fetch detection result script and image_ demo. Py parsing
- Redis master-slave replication, sentinel mode and cluster
- In order to stimulate inspiration and creativity, Shanghai daoning united with XMIND to bring you full-featured mind mapping and brainstorming software
- loading
- PHP mkdir(): Permission denied上传文件会把文件夹权限改为411权限
- (linear DP) acwing 898 Number triangle
- Oracle uses Navicat tool to import and export data
猜你喜欢

多路查找树

Lake Shore - supertran continuous flow cryogenic thermostat system

04 automatic learning rate - learning notes - lihongyi's in-depth learning 2021

SF14 | supertrend "super trend" indicator magic change and upgrade (source code)

(simple statistics) acwing 3404 Who are your potential friends

Jenkins of the integrate tool
![Divide the array into three equal parts [problem analysis]](/img/0b/856fcceb0373baa8acb46e9ae2c861.png)
Divide the array into three equal parts [problem analysis]

Lake Shore - supertran VP continuous flow cryogenic thermostat system

Solve the problem of slow downloading plug-ins for idea

Cisco private dynamic routing protocol: EIGRP
随机推荐
Chisel environment setup (win10 + vscode)
11. conditional statement if, switch
Wechat applet Bluetooth development
商品热销排行【项目 商城】
Test case design method
(linear DP | monotone queue) acwing 895 Longest ascending subsequence
删除收货地址【项目 商城】
免费分享1个新媒体运营必备的宝藏网站
DOM知识点总结
How to completely modify the user name in win10 system and win11 system
图及图的遍历
Notes on knowledge points of dynamic planning
[flume] notes
PHP mkdir(): permission denied uploading a file will change the folder permission to 411 permission
[JUC series] overview of executor framework
Shell (32): configure SSH privacy free
VS code 编写汇编代码【微机原理】
挂载smb共享提示目录无权限
Ar helps brand stores achieve global data growth
m-way search trees