当前位置:网站首页>730.Count Different Palindromic Subsequences
730.Count Different Palindromic Subsequences
2022-06-11 23:57: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.
来源:力扣(LeetCode)
链接: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
边栏推荐
- swiper
- Jetpack架构组件学习(3)——Activity Results API使用
- [day 6 of JUC learning] concurrent calculator and thread random factor
- C collection of questions for project review
- 【JUC系列】Executor框架之概览
- 挂载smb共享提示目录无权限
- MySQL some simple commands
- Divide the array into three equal parts [problem analysis]
- Teach you to play with SSM framework
- 明德扬ADC系列开发板-Ad9653子板 多通道 高分辨率 高采样率
猜你喜欢

Mingdeyang FPGA development board xilinx-k7 core board kinex7k325 410t industrial grade

Lake Shore VNF 系列低温恒温器系统——流动蒸汽中的样品

04 automatic learning rate - learning notes - lihongyi's in-depth learning 2021
![Delete the receiving address [project mall]](/img/3d/60819a1a8c36fd0c1014f91fe80470.png)
Delete the receiving address [project mall]

Solve the problem of slow downloading plug-ins for idea

HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications

Lake Shore—SuperTran-VP 连续流低温恒温器系统

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

NFS quotas:Cannot register service: RPC: Authentication error

Unity3d C # development of wechat games audio / sound playback problem solving process sharing
随机推荐
Node version control tool NVM
(dp) acwing 902. Minimum editing distance
Mingdeyang ADC series development board-ad9653 daughter board multi-channel high resolution and high sampling rate
Ar helps brand stores achieve global data growth
JS——防止自动恢复页面位置
【juc学习之路第5天】引用原子类和属性修改器
Use select to switch coroutines
Jenkins基本配置
自定义JSP标签->概念->生命周期
ETF operation record: March 1, 2022
Introduction to Solr Basics
05 classification learning notes lihongyi's in-depth study 2021
Lake Shore—SuperTran 连续流低温恒温器系统
Wechat applet Bluetooth development
Lake Shore - supertran continuous flow cryogenic thermostat system
Teach you to play with SSM framework
voc数据格式转为coco数据格式
使用 select 切换协程
Design principle [Demeter's Law]
Shell (32): configure SSH privacy free