[关闭]
@LIUHUAN 2019-05-21T09:29:03.000000Z 字数 3791 阅读 1049

字符串相关动态规划

algorithm


编辑距离

思路1

  1. int minDistance(string word1, string word2) {
  2. int m = word1.size();
  3. int n = word2.size();
  4. vector<vector<int>> dp(m+1,vector<int>(n+1,0));
  5. for(int i = 0 ;i <= m ; i++) {
  6. dp[i][0] = i;
  7. }
  8. for(int j = 0; j<= n ;j++) {
  9. dp[0][j] = j;
  10. }
  11. for(int i = 1;i <= m; i++ ) {
  12. for(int j = 1; j <= n; j++) {
  13. int t = (word1[i-1] == word2[j-1]);
  14. dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + 1;
  15. dp[i][j] = min(dp[i][j] ,dp[i-1][j-1] + 1 - t );
  16. }
  17. }
  18. return dp[m][n];
  19. }

两个字符串的删除ASCII最小值

思路1

  1. int minimumDeleteSum(string s1, string s2) {
  2. int m = s1.size();
  3. int n = s2.size();
  4. vector<vector<int>> dp(m+1,vector<int>(n+1,0));
  5. for(int i = 1;i <= m ;i++) {
  6. dp[i][0] = dp[i-1][0] + s1[i-1];
  7. }
  8. for(int j = 1; j <= n ;j++) {
  9. dp[0][j] = dp[0][j-1] + s2[j-1];
  10. }
  11. for(int i = 1; i <= m; i++) {
  12. for(int j =1 ;j <= n; j++) {
  13. if(s1[i-1] == s2[j-1])
  14. dp[i][j] = dp[i-1][j-1];
  15. else
  16. dp[i][j] = min(dp[i-1][j] + s1[i-1] ,dp[i][j-1] + s2[j-1]);
  17. }
  18. }
  19. return dp[m][n];
  20. }

两个字符串删除至相等的最小步数

思路1

  1. int minDistance(string word1, string word2) {
  2. int m = word1.size();
  3. int n = word2.size();
  4. vector<vector<int>> dp(m+1,vector<int>(n+1,0));
  5. for(int i = 1;i <= m; i++ ) {
  6. dp[i][0] = dp[i-1][0] + 1;
  7. }
  8. for(int j = 1; j <= n; j++ ) {
  9. dp[0][j] = dp[0][j-1] + 1;
  10. }
  11. for(int i = 1;i <= m;i++) {
  12. for(int j = 1;j <= n; j++) {
  13. if(word1[i-1] == word2[j-1]) {
  14. dp[i][j] = dp[i-1][j-1];
  15. }else {
  16. dp[i][j] = min(dp[i-1][j] ,dp[i][j-1]) + 1;
  17. }
  18. }
  19. }
  20. return dp[m][n];
  21. }

最长公共子序列

  1. int maxUncrossedLines(vector<int>& A, vector<int>& B) {
  2. int m = A.size();
  3. int n = B.size();
  4. vector<vector<int>> dp(m+1,vector<int>(n+1,0));
  5. for(int i = 1; i <= m; i++) {
  6. for(int j = 1; j <= n; j++) {
  7. if(A[i-1] == B[j-1])
  8. dp[i][j] = dp[i-1][j-1] + 1;
  9. else
  10. dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
  11. }
  12. }
  13. return dp[m][n];
  14. }

子序列串

思路1

  1. bool isSubsequence(string s, string t) {
  2. int m = s.size();
  3. int n = t.size();
  4. vector<vector<int>> dp(m+1,vector<int>(n+1,0));
  5. for(int j = 0; j <= n; j++ ) { // 默认空串是任何字符串的子序列
  6. dp[0][j] = 1;
  7. }
  8. for(int i = 1; i<= m; i++ ) {
  9. for(int j = 1; j <= n; j++ ) {
  10. if(s[i-1] == t[j-1]) { // s[i]==t[j]
  11. dp[i][j] = dp[i-1][j-1];
  12. } else {
  13. dp[i][j] = dp[i][j-1];
  14. }
  15. }
  16. }
  17. return dp[m][n];
  18. }

最长字符串链

思路1

  1. int longestStrChain(vector<string>& words) {
  2. sort(words.begin(),words.end(),[](string &s1,string &s2){return s1.length() < s2.length();});
  3. int n = words.size();
  4. vector<int> dp(n,1);
  5. int maxlen = 0;
  6. for(int i =1 ;i < n; i++ ) {
  7. for(int j = i-1; j >= 0; j-- ) {
  8. if( isprev(words[j] , words[i]) ) {
  9. dp[i] = max(dp[i] , dp[j] + 1);
  10. }
  11. }
  12. maxlen = max(maxlen,dp[i]);
  13. }
  14. return maxlen;
  15. }
  16. // 是否是构成链字符串的判断
  17. int isprev(string& w1,string& w2) {
  18. if(w1.length() +1 != w2.length())
  19. return 0;
  20. unordered_map<string,int> umap;
  21. int n = w2.size();
  22. int m = w1.size();
  23. for(int i = 0 ;i < n ;i++ ) {
  24. string s2(w2.begin() + i + 1 ,w2.end());
  25. string s1(w2.begin(),w2.begin() + i);
  26. if(s1+s2 == w1)
  27. return 1;
  28. }
  29. return 0;
  30. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注