13.11—动态规划—Distinct Subsequences
来源:互联网 发布:tplink访客网络是什么 编辑:程序博客网 时间:2024/06/06 18:12
描述
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 positions of the remaining characters. (ie,
"ACE" is a subsequence of "ABCDE" while "AEC" is not).
Here is an example: S = "rabbbit", T = "rabbit"
Return 3.
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 positions of the remaining characters. (ie,
"ACE" is a subsequence of "ABCDE" while "AEC" is not).
Here is an example: S = "rabbbit", T = "rabbit"
Return 3.
#include<iostream>#include<string>using namespace std;int DistinctSubsequences(string S, string T)//二维DP{int len1 = S.size();int len2 = T.size();if (len2 > len1)return 0;int **p = new int*[len1 + 1];for (int i = 0; i <= len1; i++)p[i] = new int[len2 + 1];for (int i = 0; i <= len1; i++)//初始化二维数组{for (int j = 0; j <= len2; j++){if (j == 0)p[i][j] = 1;elsep[i][j] = 0;}}for (int i = 1; i <= len1; i++){for (int j = 1; j <= len2; j++){if (S[i - 1] == T[j - 1])p[i][j] = p[i - 1][j - 1] + p[i - 1][j];elsep[i][j] = p[i - 1][j];}}int res = p[len1][len2];for (int i = 0; i <= len1; i++)delete[]p[i];delete[]p;return res;}int main(){string S = "rabbbit";string T = "rabbit";int res = DistinctSubsequences(S, T);cout << res << endl;}
阅读全文
0 0
- 13.11—动态规划—Distinct Subsequences
- 动态规划——distinct-subsequences
- LeetCode Distinct Subsequences(动态规划)
- 10069 - Distinct Subsequences(高精度+动态规划)
- 115.leetcode Distinct Subsequences(hard)[动态规划]
- LeetCode 115. Distinct Subsequences| 动态规划
- 115. Distinct Subsequences--动态规划的方法
- LeetCode(115) Distinct Subsequences(动态规划)
- LeetCode115—Distinct Subsequences
- Leetcode Distinct Subsequences 动态规划法活用总结
- Leetcode Distinct Subsequences 动态规划法活用总结
- LeetCode —— Distinct Subsequences
- LeetCode——Distinct Subsequences
- leetcode——Distinct Subsequences
- 13.11 Distinct Subsequences
- leetcode 115 —— Distinct Subsequences
- Distinct Subsequences 动态规划 没有太看懂的一题(重重重)-----重看
- 【leetcode】String——Distinct Subsequences(115)
- Spring XML解析过程
- KindEditor得不到textarea值的解决方法
- 13.10—动态规划—Decode Ways
- CentOS 6下源码安装LAMP(Linux+Apache+Mysql+Php)环境
- node中的当前目录
- 13.11—动态规划—Distinct Subsequences
- 大型信息系统的技术选型
- E
- vue + webpack 安装和配置
- 13.12—动态规划—Word Break
- 考研英语
- [VC.NET]句柄
- 线段树学习总结
- 倒角距离匹配