Leetcode Distinct Subsequences 动态规划法活用总结

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.

动态规划法活用总结:
1 二维表:很多问题都可以转化为二维表
2 三维表:比较难理解,时间效率一般是O(n*n*n)
3 二维表变一维表:一般是简化二维表,不需要保存整个表
4 一维表变常量:一般是由一维表简化而来,一维表也不需要保存,只保存当前结果和当前结果需要用的变量就可以
5 逆向填表:根据表进一步抽象简化出来,可以让程序更加简洁,当然也更加更加难理解。


设计动态规划法算法,应该循序渐进,先设计二维表或三维表,然后再简化,进一步优化,再简化,不要一步就写最简化的程序。

熟练之后,可以直接设计表,根据表特征,翻译为程序。

熟练递归回溯法有助于动态规划法的深入理解。

  1. //最好理解的二维动态规划法,一次性ac。  
  2.     int numDistinct2(string S, string T)   
  3.     {  
  4.         vector<vector<int> > ta(T.size()+1, vector<int>(S.size()+1));  
  5.         for (int i = 0; i <= S.size(); i++)  
  6.         {  
  7.             ta[0][i] = 1;  
  8.         }  
  9.         for (int i = 1; i <= T.size(); i++)  
  10.         {  
  11.             for (int j = 1; j <= S.size(); j++)  
  12.             {  
  13.                 if (T[i-1] == S[j-1]) ta[i][j] = ta[i][j-1] + ta[i-1][j-1];  
  14.                 else ta[i][j] = ta[i][j-1];  
  15.             }  
  16.         }  
  17.         return ta[T.size()][S.size()];  
  18.     }  


  1. //转置行和列的1维表动态规划法-优化  
  2.     int numDistinct(string S, string T)   
  3.     {  
  4.         vector<int> ta(T.size()+1);  
  5.         int a = 0, b = 1;  
  6.         for (int i = 1; i <= S.size(); i++)  
  7.         {  
  8.             b = 1;  
  9.             for (int j = 1; j <= T.size(); j++)  
  10.             {  
  11.                 a = ta[j];  
  12.                 if (S[i-1] == T[j-1]) ta[j] += b;  
  13.                 b = a;  
  14.                 if (b == 0) break;  
  15.             }  
  16.         }  
  17.         return ta[T.size()];  
  18.     }  


  1. //神奇的通过的程序--逆向填表法  
  2.     int numDistinct2(string S, string T) {  
  3.         vector<int> ta(T.size()+1);  
  4.         ta[0] = 1;  
  5.         for (int i = 0; i < S.size(); i++)  
  6.         {  
  7.             for (int j = T.size()-1; j >= 0; j--)  
  8.             {  
  9.                 ta[j+1] += (S[i]==T[j])*ta[j];  
  10.             }  
  11.         }  
  12.         return ta[T.size()];  
  13.     }  


  1. //2014-2-17 update  
  2.     int numDistinct(string S, string T)   
  3.     {  
  4.         int *table = new int[T.length()+1];  
  5.         table[0] = 1;  
  6.         for (int i = 1; i <= T.length(); i++)  
  7.         {  
  8.             table[i] = 0;  
  9.         }  
  10.         for (int i = 0; i < S.length(); i++)  
  11.         {  
  12.             int j = i<T.length()?i:T.length();  
  13.             for (; j >= 0; j--)  
  14.             {  
  15.                 if (T[j] == S[i]) table[j+1] += table[j];//把握全局,逆向填表  
  16.             }  
  17.         }  
  18.         return table[T.length()];  
  19.     }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值