博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode Week11 Distinct Subsequences
阅读量:4466 次
发布时间:2019-06-08

本文共 1449 字,大约阅读时间需要 4 分钟。

Question

Given a string S and a string T, count the number of distinct subsequences of S which equals T.

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

Example 1:

Input: S = "rabbbit", T = "rabbit"Output: 3Explanation:As shown below, there are 3 ways you can generate "rabbit" from S.(The caret symbol ^ means the chosen letters)rabbbit^^^^ ^^rabbbit^^ ^^^^rabbbit^^^ ^^^

Example 2:

Input: S = "babgbag", T = "bag"Output: 5Explanation:As shown below, there are 5 ways you can generate "bag" from S.(The caret symbol ^ means the chosen letters)babgbag^^ ^babgbag^^    ^babgbag^    ^^babgbag  ^  ^^babgbag    ^^^

Answer

class Solution {public:    int numDistinct(string s, string t) {         vector
> dp(t.size(), vector
(s.size())); int dis = 0; for (int i = 0; i < t.size(); i++) { dis = 0; for (int j = 0; j < s.size(); j++) { dp[i][j] = dis; if (t[i] == s[j]) { if (i == 0) dis++; else if (j != 0) dis = dp[i - 1][j] + dp[i][j]; } } } return dis; }};

  动态规划

转载于:https://www.cnblogs.com/thougr/p/10207035.html

你可能感兴趣的文章
[Unity插件]Lua行为树(五):装饰节点Repeater
查看>>
顺序表、链表、栈和队列
查看>>
Linux第二天(Linux常用命令2)
查看>>
MySql知识体系
查看>>
JIRA中的标记语言的语法参考
查看>>
hdu 6318 Swaps and Inversions(归并排序)
查看>>
用css在IE7、8上实现圆角
查看>>
三维绿幕标定与跟踪
查看>>
android ProgressBar自定义半圆形进度条
查看>>
hdu.5212.Code(莫比乌斯反演 && 埃氏筛)
查看>>
python学习记录一
查看>>
IP通信基础 4月1日
查看>>
KeyProvider
查看>>
空指针为什么能调用成员函数?
查看>>
用MySQL的存储过程来实现一些经典函数
查看>>
React (2) -- State and Lifecycle
查看>>
【转】在EmEditor上编译并运行JAVA
查看>>
关于SqlDateTime溢出的问题
查看>>
jquery下php与ajax的数据交换方式
查看>>
魅蓝Note有几种颜色 魅蓝Note哪个颜色好看
查看>>