LN : leetcode 730 Count Different Palindromic Subsequences

lc 730 Count Different Palindromic Subsequences



730 Count Different Palindromic Subsequences

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_i.

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‘}.

带记忆数组 Accepted

虽然题目只要求四个字母,但我们扩展普遍性,这里就做二十六个字母的。带记忆数组和动态规划的本质是差不多的。带记忆数组memo的递归解法,这种解法的思路是一层一层剥洋葱,比如"bccb",按照字母来剥,先剥字母b,确定最外层"b _ _ b",这会产生两个回文子序列"b"和"bb",然后递归进中间的部分,把中间的回文子序列个数算出来加到结果res中,然后开始剥字母c,找到最外层"cc",此时会产生两个回文子序列"c"和"cc",然后由于中间没有字符串了,所以递归返回0,按照这种方法就可以算出所有的回文子序列了。

class Solution {
public:
    int countPalindromicSubsequences(string S) {
        int len = S.size();
        vector<vector<int>> dp(len+1, vector<int>(len+1, 0));
        vector<vector<int>> ch(26, vector<int>());
        for (int i = 0; i < len; i++) {
            ch[S[i]-‘a‘].push_back(i);
        }
        return calc(S, ch, dp, 0, len);
    }
    int calc(string S, vector<vector<int>>& ch, vector<vector<int>>& dp, int start, int end) {
        if (start >= end) return 0;
        if (dp[start][end] > 0) return dp[start][end];
        long ans = 0;
        for (int i = 0; i < 26; i++) {
            if (ch[i].empty()) continue;
            auto new_start = lower_bound(ch[i].begin(), ch[i].end(), start);
            auto new_end = lower_bound(ch[i].begin(), ch[i].end(), end) - 1;
            if (new_start == ch[i].end() || *new_start >= end)  continue;
            ans++;
            if (new_start != new_end)   ans++;
            ans += calc(S, ch, dp, *new_start+1, *new_end);
        }
        dp[start][end] = ans % int(1e9+7);
        return dp[start][end];
    }
};

原文地址:https://www.cnblogs.com/renleimlj/p/8058943.html

时间: 2024-10-10 07:46:46

LN : leetcode 730 Count Different Palindromic Subsequences的相关文章

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

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

LeetCode:Count and Say

1.题目名称 Count and Say(按照数字重复出现计数并生成字符串) 2.题目地址 https://leetcode.com/problems/count-and-say/ 3.题目内容 英文:The count-and-say sequence is the sequence of integers beginning as follows 中文:给出正整数n,返回"count-and-say"序列的第n项 说明: count-and-say序列形如:1, 11, 21, 1

LeetCode:Count Primes - 统计质数数量

1.题目名称 Count Primes(统计质数数量) 2.题目地址 https://leetcode.com/problems/count-primes/ 3.题目内容 英文:Count the number of prime numbers less than a non-negative number, n. 中文:统计正整数n以内(不含n本身)质数的数量 4.一个TLE的方法 从1到n,考察每个数字是否为质数.这个方法由于花费时间较长,不能满足题目中对时间的要求. 一段实现此方法的Jav

Leetcode problem-204 Count Primes 题解

Leetcode problem-204 Count Primes Count the number of prime numbers less than a non-negative number, n. 题解:这道题如果对每个小于n的数都进行判断是否为素数并计数会超时,因此采用筛法来解这题.建一个数组,从2开始, 把其倍数小于N的都删掉. class Solution { public: int countPrimes(int n) { vector<int>arr(n,1); int s

[LeetCode] 038. Count and Say (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Count and Say (Easy) 链接: 题目:https://leetcode.com/problems/Count-and-Say/ 代码(github):https://github.com/illuz/leetcode 题意: 数数,第一个是 1,第二个是数前一个数:1 个 1,就是 11

LeetCode之“动态规划”:Distinct Subsequences

题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing th

【LeetCode OJ】Longest Palindromic Substring

题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 解题思路:p

8.8 LeetCode 222 Count Complete Tree Nodes

Question: Count Complete Tree Nodes Total Accepted: 11040 Total Submissions: 53992My Submissions Question Solution Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree

LeetCode(3)题解: Longest Palindromic Substring

https://leetcode.com/problems/longest-palindromic-substring/ 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路: 我的思路是遍