【LeetCode】Distinct Subsequences
2014-04-15 20:10
309 查看
参考链接
http://blog.csdn.net/xshalk/article/details/8223120http://blog.csdn.net/bigapplestar/article/details/17523391
http://blog.csdn.net/jellyyin/article/details/9060709
题目描述
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 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.
题目分析
题意:给定2个字符串a, b,求b在a中出现的次数。要求可以是不连续的,但是b在a中的顺序必须和b以前的一致。
示例说明:
S = "rabbbit", T = "rabbit"
为了区分3个b,我们写作b0b1b3
T在S中出现了3分。分别是rab0b1it,rab0b2it,rab1b2it
思路:动态规划的关键就是找递归公式。
类似于数字分解的题目。dp[i][j]表示:b的前j个字符在a的前i个字符中出现的次数。
如果a[i] == b[j]则 dp[i][j] = dp[i-1][j] + dp[i-1][j-1]
如果a[i] != b[j]则 dp[i][j] = dp[i-1][j]
总结
这个一个典型的动态规划题,需要多多复习。代码示例
#include #include using namespace std; void printvec(vector A) { for(int i = 0;i > A, char *name) { printf("%s\n",name); for(int i = 0;i > num(S.size()+1,vector(T.size()+1)); for(int i = 0; i<=S.size(); i++){ num[i][0] = 0; } for(int j=0; j<=T.size(); j++){ num[0][j] = 0; } for(int i = 1; i<=S.size(); i++){ for(int j = 1; j<=i && j<=T.size(); j++){ num[i][j] = num[i-1][j]; if(S[i-1] == T[j-1]){ if(j == 1){ num[i][j] += 1; }else{ num[i][j] += num[i-1][j-1]; } } } } //printvecvec(num,"ret"); return num[S.size()][T.size()]; } }; #elif 1 class Solution { public: int numDistinct(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function int len_s = S.size(); int len_t = T.size(); vector > f(len_s+1,vector(len_t+1)); if(len_s < len_t) return 0; for(int i = 0 ; i <= len_s ; i++) f[i][0] = 0; for(int j = 0 ; j <= len_t ; j++) f[0][j] = 0; for(int i = 1 ; i <= len_s ;i++) if(S[i-1]==T[0]) f[i][1] = f[i-1][1] + 1; else f[i][1] = f[i-1][1]; for(int i = 2; i <= len_s ; i++) for(int j = 2; j <= len_t ; j++) if(S[i-1]==T[j-1]) f[i][j] = f[i-1][j-1] + f[i-1][j]; else f[i][j] = f[i-1][j]; return f[len_s][len_t]; } }; #endif void test0() { Solution so; if(so.numDistinct("rabbbit","rabbit") != 3) printf("------------------------failed\n"); else printf("------------------------passed\n"); } int main(int argc, char *argv[]) { test0(); return 0; }
推荐学习C++的资料
C++标准函数库http://download.csdn.net/detail/chinasnowwolf/7108919
在线C++API查询
http://www.cplusplus.com/
相关文章推荐
- UESTC 250 windy数(数位DP)
- 百度面试题query的分类排序
- easyui 打开多个tab页签,切换到第一次打开的页签时,其内容总是别的页签内容解决办法。
- leetcode-Longest Consecutive Sequence(2014.3.31)
- iTween.ValueTo实现tk2d的sprite渐隐效果
- 华为机试——五分制成绩:break和continue的区分
- CBuilder6安装后运行报错,打开工程选项崩溃的处理方法—Win 2008R2或Win7
- Uva11324 The Largest Clique tarjan+dp
- iosUI开发的一些随笔记
- 66款 Android UI 组件开源软件
- mysql数据类型之用 TINYINT(1) 还是 ENUM( 'true' , 'false')? 推荐
- UVA 10405 LONGEST COMMON SUBSEQUENCE(最长公共子序列)
- ios学习记录 day32 UI 9+ 多视图切换 传值
- iOS开发 UIViewController
- 苹果设备码 科普 UDID UUID advertisingIdentifier
- mysql 连接url中useUnicode=true & characterEncoding=UTF-8 的作用
- RH413--在RHEL6.4下测试nosuid和noexec选项
- UIScrollView 常用的一些方法
- 使用addChildViewController手动控制UIViewController的切换
- 数据库查询构建控件集Active Query Builder