Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.

A subsequence of a string S is obtained by deleting 0 or more characters from S.

A sequence is palindromic if it is equal to the sequence reversed.

Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_

Example 1:

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.

Example 2:

Input:
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output: 104860361
Explanation:
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.

Note:

  • The length of S will be in the range [1, 1000].
  • Each character S[i] will be in the set {'a', 'b', 'c', 'd'}.

Approach #1: DFS + Memeory. [C++][MLE]

class Solution {
public:
int countPalindromicSubsequences(string S) {
return count(S);
} private:
unordered_map<string, long> memo;
static constexpr long mod = 1000000007; int count(const string& s) {
if (s.empty()) return 0;
if (s.length() == 1) return 1;
if (memo[s] > 0) return memo[s];
int len = s.length();
long ans = 0;
if (s[0] == s[len-1]) {
int l = 1, r = len - 2;
while (l <= r && s[l] != s[0]) l++;
while (l <= r && s[r] != s[len-1]) r--;
if (l > r) ans = count(s.substr(1, len-2))*2 + 2;
else if (l == r) ans = count(s.substr(1, len-2))*2 + 1;
else ans = count(s.substr(1, len-2))*2 - count(s.substr(l+1, r-l-1));
} else {
ans = count(s.substr(0, len-1)) + count(s.substr(1, len-1)) - count(s.substr(1, len-2));
} ans = (ans + mod) % mod;
// cout << ans << endl; return memo[s] = ans;
}
};

  

Approach #2: Optimization. [C++]

class Solution {
public:
int countPalindromicSubsequences(string S) {
int len = S.length();
memo = vector<int>(len*(len+1)+1, 0);
return count(S, 0, len-1);
} private:
vector<int> memo;
static constexpr long mod = 1000000007; int count(const string& S, int s, int e) {
if (s > e) return 0;
if (s == e) return 1;
int key = s * S.length() + e;
if (memo[key] > 0) return memo[key];
int len = S.length();
long ans = 0;
if (S[s] == S[e]) {
int l = s+1, r = e-1;
while (l <= r && S[l] != S[s]) l++;
while (l <= r && S[r] != S[e]) r--;
if (l > r) ans = count(S, s+1, e-1)*2 + 2;
else if (l == r) ans = count(S, s+1, e-1)*2 + 1;
else ans = count(S, s+1, e-1)*2 - count(S, l+1, r-1);
} else {
ans = count(S, s+1, e) + count(S, s, e-1)
- count(S, s+1, e-1);
} return memo[key] = (ans + mod) % mod;
}
};

  

Approach #3: DP. [C++]

class Solution {
long mod = 1000000007;
public int countPalindromicSubsequences(String S) {
int len = S.length();
long[][] dp = new long[len][len];
for (int i = 0; i < len; ++i)
dp[i][i] = 1;
for (int k = 1; k <= len; ++k) {
for (int i = 0; i < len-k; ++i) {
int j = i + k;
if (S.charAt(i) == S.charAt(j)) {
dp[i][j] = dp[i+1][j-1] * 2;
int l = i + 1;
int r = j - 1;
while (l <= r && S.charAt(l) != S.charAt(i)) l++;
while (l <= r && S.charAt(r) != S.charAt(j)) r--;
if (l > r) dp[i][j] += 2;
else if (l == r) dp[i][j] += 1;
else dp[i][j] -= dp[l+1][r-1];
} else
dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1];
dp[i][j] = (dp[i][j] + mod) % mod;
}
}
return (int)dp[0][len-1];
}
}

  

Reference:

http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-730-count-different-palindromic-subsequences/

730. Count Different Palindromic Subsequences的更多相关文章

  1. leetcode 730 Count Different Palindromic Subsequences

    题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...

  2. LN : leetcode 730 Count Different Palindromic Subsequences

    lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a ...

  3. [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  4. 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...

  5. [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  6. [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  7. Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

随机推荐

  1. Spring框架的AOP的底层实现之JDK的动态代理(代码了解,理解原理)

    1.创建接口UserDao: package com.huida.demo1; public interface UserDao { public void save(); public void u ...

  2. windows下 Memcache cpu占用过高

    分析应该是memcache的内存大小还是默认配置,已经满足不了当前的大数据量的需要了,大量的新缓存需要进入,同时大量的旧缓存又需要被淘汰出来,一进一出导致CPU占用过多. 进入注册表,找到:HKEY_ ...

  3. FutureTask详解

    1 基本概念 1.1 Callable与Future Runnable封装一个异步运行的任务,可以把它想象成为一个没有参数和返回值的异步方法.Callable与Runnable类似,但是有返回值.Ca ...

  4. tp5链接访问

    方法名:admin/DayActive/statistic 访问:admin/day_active/statistic

  5. 支持向量机(SVM)算法

    支持向量机(support vector machine)是一种分类算法,通过寻求结构化风险最小来提高学习机泛化能力,实现经验风险和置信范围的最小化,从而达到在统计样本量较少的情况下,亦能获得良好统计 ...

  6. 2018.08.14 bzoj4241: 历史研究(回滚莫队)

    传送们 简单的回滚莫队,调了半天发现排序的时候把m达成了n... 代码: #include<bits/stdc++.h> #define N 100005 #define ll long ...

  7. arduino 串口数据啊按字节分析

    #include <avr/wdt.h> #include <SoftwareSerial.h> #include <EEPROM.h> #define FPIN ...

  8. cordova 选择图片并上传到服务器

    js navigator.camera.getPicture(function(imageURI){ var url=apiUrl+"/upload.aspx"; //alert( ...

  9. SpringMVC零碎笔记

    在web.xml里可以配置webapp的默认首页,格式如下: <welcome-file-list> <welcome-file>index.html</welcome- ...

  10. 简单的Java,Python,C,C++

    Java 语言 //package main //注意不要添加包名称,否则会报错. import java.io.*; import java.util.*; cin.hasNext(); cin.h ...