Distinct Subsequences----匹配个串串

题目大意

给定母串S和待匹配串T,求T能在母串S中匹配多少次(不一定要连续匹配,并且母串中多个相同字母可以依次使用,但均只能使用一次,如S=rabbbit  T=rabbit, 则S中三个连续的b可以依次匹配T中两个bb各一次())

解题思路

字符串匹配并且要求结果只为一个数字的时候,很多情况都是使用DP,这题也不例外,建立数组dp[i][j]表示按题目要求当前T串前i个字符匹配S串前j个字符的匹配结果。

建立转移方程:

当T[i] != S[j] 时,显然其值不会改变,因此dp[i][j] = dp[i][j-1];

当T[i] == S[j] 时,此时可以考虑从两个方向转移到当前dp[i][j];

第一个方向:dp[i-1][j-1],将T[0...(i-1)]记为T‘ ,表示在T‘尾部增加一个当前字符;

第二个方向:dp[i][j-1],将T[0...(i)]记为T‘‘
,表示替换掉T’‘尾部的一个字符(必与当前字符相同);

由于整个过程只用从T的前一步状态转移而来,因此使用一个2*n(n为母串S的长度)的二维数组就能实现dp全过程。

总结

重点考虑匹配串T,分析其每扩展一个字符后,可由哪些原来的状态转移到新的当前状态来。这道题目想到了就不是很难,主要是多了一些不必要的判断,不过为了保证程序易读且正确,加上这些判断也是值得的,可以后期逐步求精。

代码

class Solution {
public:
    int numDistinct(string S, string T)
    {

        int n = S.length();
        int m = T.length();
        if (m > n)return 0;
        int dp[2][11000];
        bool jud = false;
        memset(dp, 0, sizeof(dp));
        int index = 0;
        for (int i = 0; i < n; i ++){
            if (S[i] == T[0]){
               if (!jud){
                  jud = true;
                  dp[index][i] = 1;
               }
               else dp[index][i] = dp[index][i-1]+1;
            }
            else if (i)  dp[index][i] = dp[index][i-1];
        }
        for (int i = 1; i < m; i ++){
            jud = false;
            index^=1;

            for (int j = i; j < n; j ++){
                if (T[i] == S[j]){
                   if (!jud){
                      jud = true;
                      dp[index][j] = dp[index^1][j-1];
                   }
                   else  dp[index][j] = dp[index^1][j-1]+dp[index][j-1];
                }
                else dp[index][j] = dp[index][j-1];

            }
            for (int j = 0; j < n; j ++)   dp[index^1][j] = 0;
        }
        return dp[index][n-1];
    }
};
时间: 2024-11-03 23:20:44

Distinct Subsequences----匹配个串串的相关文章

[LeetCode]Distinct Subsequences 匹配(不要求连续)的子串

一开始第一反映是用暴搜+回溯剪枝,妥妥的超时,见numDistinct0函数. 后来想到这跟公共子串有点类似,满足最优子结构和重叠问题,因此可用DP. 状态转移方程如下: { dp[i-1,j-1]+dp[i-1][j] , 当s[i]==s[j],0<i<s.size(),0<j<t.size(), dp[i,j]={ dp[i-1][j],                     当s[i]!=s[j] ,0<i<s.size(),0<j<t.size(

115. Distinct Subsequences *HARD* -- 字符串不连续匹配

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 the relative

Distinct Subsequences

https://leetcode.com/problems/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 non

Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

Given a string S and a string T, count the number of distinct subsequences ofT inS. 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 the relative po

LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

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 di

【Distinct Subsequences】cpp

题目: 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 the relat

LeetCode: Distinct Subsequences 解题报告

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 di

Distinct Subsequences leetcode java

题目: 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 the relat

Leetcode 115 Distinct Subsequences 解题报告

Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solution 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

【leetcode】Distinct Subsequences(hard)

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 the relative