<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    IT技術小屋

    秋風秋雨,皆入我心

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      38 隨筆 :: 1 文章 :: 19 評論 :: 0 Trackbacks
    Distinct Subsequences

    Given a string S and a string T, count the number of distinct sub sequences 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.

    這兩個題目很相似,狀態方程是f(i, j) = f(i - 1, j) + S[i] == T[j]? f(i - 1, j - 1) : 0 Where f(i, j) is the number of distinct sub-sequence for T[0:j] in S[0:i].
    數組初始情況是第一列全部是1,代表T字符串是空串,S和自己做匹配。實現代碼如下:
     1 public class DistinctSubsequences {
     2     public int numDistinct(String S, String T) {
     3         int[][] f = new int[S.length() + 1][T.length() + 1];
     4         for (int k = 0; k < S.length(); k++)
     5             f[k][0] = 1;
     6         for (int i = 1; i <= S.length(); i++) {
     7             for (int j = 1; j <= T.length(); j++) {
     8                 if (S.charAt(i - 1) == T.charAt(j - 1)) {
     9                     f[i][j] += f[i - 1][j] + f[i - 1][j - 1];
    10                 } else {
    11                     f[i][j] += f[i - 1][j];
    12                 }
    13             }
    14         }
    15         return f[S.length()][T.length()];
    16     }
    17 }

    Edit Distance
    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
    You have the following 3 operations permitted on a word:
    a) Insert a character
    b) Delete a character
    c) Replace a character

     1 public class EditDistance {
     2     public int minDistance(String word1, String word2) {
     3         if (word1.length() == 0 || word2.length() == 0)
     4             return word1.length() == 0 ? word2.length() : word1.length();
     5         int[][] arr = new int[word2.length() + 1][word1.length() + 1];
     6         for (int i = 0; i <= word1.length(); i++) {
     7             arr[0][i] = i;
     8         }
     9         for (int j = 0; j <= word2.length(); j++) {
    10             arr[j][0] = j;
    11         }
    12         for (int i = 0; i < word1.length(); i++) {
    13             for (int j = 0; j < word2.length(); j++) {
    14                 if (word1.charAt(i) == word2.charAt(j)) {
    15                     arr[j + 1][i + 1] = arr[j][i];
    16                 } else {
    17                     int dis = Math.min(arr[j][i + 1], arr[j + 1][i]);
    18                     arr[j + 1][i + 1] = Math.min(arr[j][i], dis) + 1;
    19                 }
    20             }
    21         }
    22         return arr[word2.length()][word1.length()];
    23     }
    24 }
    posted on 2013-12-31 10:21 Meng Lee 閱讀(246) 評論(0)  編輯  收藏 所屬分類: Leetcode
    主站蜘蛛池模板: 香港经典a毛片免费观看看| 久久亚洲精品中文字幕三区| 午夜视频在线观看免费完整版| 99爱视频99爱在线观看免费| 国产亚洲人成无码网在线观看| 在线观看亚洲天天一三视| 久久久久久久亚洲精品| 亚洲自偷自偷精品| 在线观看日本亚洲一区| 伊人久久国产免费观看视频| 99久久免费观看| 91免费国产在线观看| 国产成人免费a在线视频app| 亚洲人成网站在线观看播放| 亚洲av无码国产精品色午夜字幕 | 99视频在线免费| 丁香花免费完整高清观看| 国产国产人免费视频成69大陆| 亚洲日韩av无码| 亚洲an日韩专区在线| 国产亚洲综合成人91精品| 亚洲三级中文字幕| 日韩在线一区二区三区免费视频| 3344在线看片免费| 国产成人免费a在线视频色戒| 亚洲国产美国国产综合一区二区| 亚洲风情亚Aⅴ在线发布| 久久久久久夜精品精品免费啦| 免费在线观看一级毛片| 久久精品国产亚洲AV无码麻豆| 国产成人精品亚洲一区| 免费观看国产网址你懂的| 亚洲精品老司机在线观看| 亚洲一区二区久久| 中文成人久久久久影院免费观看| 国产大片91精品免费看3| 亚洲成综合人影院在院播放| 久久国产一片免费观看| 国产jizzjizz免费看jizz| 亚洲国产成人久久| 国产va在线观看免费|