Distinct Subsequences
来源:互联网 发布:stussy淘宝哪家正 编辑:程序博客网 时间:2024/06/18 14:04
题目描述:
Given a string S and a string T, count the number of distinct subsequences ofT 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
.
public class Solution { public int numDistinct(String s, String t) { return getNumDistinct(s, 0, t, 0); } public int getNumDistinct(String s, int sStart, String t, int tStart){ if(s.length()-sStart<t.length()-tStart){ return 0; } if(tStart==t.length()) return 1; if(sStart==s.length()){ return 0; } if(s.charAt(sStart)!=t.charAt(tStart)){ return getNumDistinct(s, sStart+1, t, tStart); }else{ return getNumDistinct(s, sStart+1, t, tStart+1)+getNumDistinct(s, sStart+1, t, tStart); } }}然后就是在这里加入备忘录,用dp记录下s[sStart, s.length-1]中包含t[tStart, t.length-1]的子串数。
时间性能增强很多,但是会出现这个错误:Line 32: java.lang.StackOverflowError。内存溢出,也就是递归调用次数多了。还是得转换成自下而上的dp算法才行!
public class Solution { public int numDistinct(String s, String t) {int slen=s.length(),tlen=t.length();int[][] dp=new int[slen+1][tlen+1];for(int i=0;i<=slen;i++){for(int j=0;j<=tlen;j++){dp[i][j]=Integer.MAX_VALUE;}} int num= getNumDistinct(s, 0, t, 0, dp); return num; }public int getNumDistinct(String s, int sStart, String t, int tStart, int[][] dp){if(s.length()-sStart<t.length()-tStart){dp[sStart][tStart]=0;return 0;}if(tStart==t.length()){dp[sStart][tStart]=1;return 1;}if(sStart==s.length()){dp[sStart][tStart]=0;return 0;}if(s.charAt(sStart)!=t.charAt(tStart)){if(dp[sStart+1][tStart]!=Integer.MAX_VALUE){dp[sStart][tStart]=dp[sStart+1][tStart];return dp[sStart+1][tStart];}else{int a = getNumDistinct(s, sStart+1, t, tStart, dp);dp[sStart][tStart]=a;return a;}}else{int dp1=0,dp2=0;if(dp[sStart+1][tStart+1]!=Integer.MAX_VALUE){dp1=dp[sStart+1][tStart+1];}else{dp1=getNumDistinct(s, sStart+1, t, tStart+1, dp);}if(dp[sStart+1][tStart]!=Integer.MAX_VALUE){dp2=dp[sStart+1][tStart];}else{dp2=getNumDistinct(s, sStart+1, t, tStart, dp);}dp[sStart][tStart]=dp1+dp2;return dp1+dp2;}}}AC代码如下:
动态规划,定义dp[i][j]为字符串i变换到j的变换方法。
如果S[i]==T[j],那么dp[i][j] = dp[i-1][j-1] + dp[i-1][j]。意思是:如果当前S[i]==T[j],那么当前这个字母即可以保留也可以抛弃,所以变换方法等于保留这个字母的变换方法加上不用这个字母的变换方法。
如果S[i]!=T[i],那么dp[i][j] = dp[i-1][j],意思是如果当前字符不等,那么就只能抛弃当前这个字符。
递归公式中用到的dp[0][0] = 1,dp[i][0] = 0(把任意一个字符串变换为一个空串只有一个方法)
public class Solution { public int numDistinct(String S, String T) { if(S==null||T==null) { return 0; } if(S.length()<T.length()) { return 0; } int [][] dp = new int[S.length()+1][T.length()+1]; dp[0][0] = 1; for(int i=0;i<S.length();i++) { dp[i][0] = 1; } for(int i=1;i<=S.length();i++) { for(int j=1;j<=T.length();j++) { dp[i][j] = dp[i-1][j]; if(S.charAt(i-1)==T.charAt(j-1)) { dp[i][j] += dp[i-1][j-1]; } } } return dp[S.length()][T.length()]; } }
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- Distinct Subsequences
- SuperMap iDesktop 8C使用KingbaseES数据库
- BlocksKit的使用
- .net保存html文件
- myeclipse中java.lang.OutOfMemoryError: PermGen space
- hadoop2.7.1 的集群搭建
- Distinct Subsequences
- 修改windows的注册表以实现修改键盘按键的映射
- java基础集合之Map
- Java学习笔记(17) Binary I/O
- 使用SQLyog连接MySQL
- Directx11 6.15EXERCISES(给自己看的)
- 二维透视投影变换
- Hibernate Dao、Service 基类
- extern