leetcode-distinct sequences
2024-08-30 05:07:16
Solution: when see question about two strings , DP should be considered first.
We can abstract this question to calculate appear times for string T with length i in string S with length j, which can be represented by numbers[i][j], then through observation and thinking , we can know for numbers[i][j] it should at least equal the numbers[i][j-1] and if T.charAt(i)==S.charAt(j) , numbers[i][j] should also be add numbers[i-1][j-1]
class Solution
{
public:
int numDistinct(string S, string T) {
int sLen = S.length(); int tLen = T.length();
vector<vector<int>> dp(sLen+,vector<int>(tLen+));//dp[i][j]表示对应S前i个和T前j个字符的子问题。
for (int i = ; i <= sLen; i++) dp[i][] = ;
for (int i = ; i <= sLen; i++)
{
for (int j = ; j <= tLen; j++)
{
if (S[i - ] == T[j - ])
{
dp[i][j] = dp[i - ][j] + dp[i-][j-];
}
else
{
dp[i][j] = dp[i-][j];
}
}
}
return dp[sLen][tLen];
} };
int main()
{
Solution s;
string strS("b");
string strT("a");
cout << s.numDistinct(strS, strT) << endl;
return ;
}
http://rleetcode.blogspot.com/2014/01/distinct-subsequences-java.html
最新文章
- 深入理解javascript原型和闭包(9)——简述【执行上下文】下
- MySQL检查重复索引工具-pt-duplicate-key-checker
- 彻底解决m2eclipse之Unable to update index for central
- 【php】使用phpdbg来调试php程序
- Open经验库网址
- Android中级之网络数据解析一之xml解析
- Linux 查看版本详情
- 关于C#正则表达式MatchCollection类的总结,正则表达式的应用
- chrome开发工具指南(六)
- 【转载】基于VoiceOver的移动web站无障碍访问实战
- poj-2909-哥德巴赫猜想
- centos7.2安装完成的基本操作
- Kettle Spoon入门教程
- eclipse下安装windowbuilder(一定要看)
- markdown的css样式(自己写的)
- 对CountDownLatch的初步学习
- win8 下面 listen 的队列长度貌似无效了 上c/s 代码 并附截图,有图有真相
- 隐藏windows任务栏中的窗口显示
- python基础之字符串格式化
- 自适应滤波:维纳滤波器——FIR及IIR设计
热门文章
- Html checkbox全选
- cv2.FileNode has no keys
- sql 获取当天开始时间 结束时间
- 基于web端去除空格小工具
- Android创建定时和周期任务
- MySQL通过SQL语句来直接生成新表
- shell之“>;/dev/null 2>;&;1” 详解
- React.js 小书 Lesson26 - 实战分析:评论功能(五)
- [转]asp.net5中使用NLog进行日志记录
- 三年从前端小工到架构-知乎 Live 学习整理