Change Dir

          先知cd——熱愛生活是一切藝術(shù)的開始

          統(tǒng)計

          留言簿(18)

          積分與排名

          “牛”們的博客

          各個公司技術(shù)

          我的鏈接

          淘寶技術(shù)

          閱讀排行榜

          評論排行榜

          總結(jié)一下幾天前的LCS算法編寫

          沒有過多的技術(shù)含量,只是拿來分享一下,希望不要被手懶的各位學生朋友直接拿去做作業(yè)。大家有興趣可以和我探討,最近看來有時間可以做些研究工作。

          不說了,參考文章去百度搜一下吧,最長公共子串。

          DP解:

             1: /**
             2:      * 動態(tài)規(guī)劃求最長公共子串
             3:      * 
             4:      * @param s
             5:      * @param t
             6:      * @return
             7:      */
             8:     public String getLCSByDP(String s, String t) {
             9:         int len_s = s.length();
            10:         int len_t = t.length();
            11:         String[][] num = new String[len_s][len_t];
            12:         char ch1 = '\0';
            13:         char ch2 = '\0';
            14:         int len = 0;
            15:         String res = "";
            16:         for (int i = 0; i < len_s; i++) {
            17:             for (int j = 0; j < len_t; j++) {
            18:                 ch1 = s.charAt(i);
            19:                 ch2 = t.charAt(j);
            20:                 if (ch1 != ch2) {
            21:                     num[i][j] = "";
            22:  
            23:                 } else {
            24:                     if (i == 0 || j == 0) {
            25:                         num[i][j] = ch1 + "";
            26:                     } else {
            27:                         num[i][j] = num[i - 1][j - 1] + ch1;
            28:                     }
            29:                     if (num[i][j].length() > len) {
            30:                         len = num[i][j].length();
            31:                         res = num[i][j];
            32:                     }
            33:                 }
            34:             }
            35:         }
            36:         return res;
            37:     }

           

          GST解:

             1: public static final String tail1 = "$1";
             2:     public static final String tail2 = "$2";
             3:  
             4: /**
             5:      * 求最長公共前綴LCP
             6:      * 
             7:      * @param word1
             8:      * @param word2
             9:      * @return
            10:      */
            11:     public String getLCP(String word1, String word2) {
            12:         String res = "";
            13:         int len1 = word1.length();
            14:         int len2 = word2.length();
            15:         for (int i = 0; i < Math.min(len1, len2); i++) {
            16:             if (word1.charAt(i) == word2.charAt(i)) {
            17:                 res += word1.charAt(i);
            18:             } else
            19:                 break;
            20:         }
            21:         return res;
            22:     }
            23:  
            24:     /**
            25:      * 后綴數(shù)組方法求最長公共子串
            26:      * 
            27:      * @param word1
            28:      * @param word2
            29:      * @return
            30:      */
            31:     public String getLCSByGST(String word1, String word2) {
            32:         String res = "";
            33:         
            34:         int len1 = word1.length();
            35:         int len2 = word2.length();
            36:         String gst[] = new String[len1 + len2];
            37:         for (int i = 0; i < len1; i++) {
            38:             gst[i] = mergeSuffix(word1.substring(i), word2);
            39:         }
            40:         for (int i = 0; i < len2; i++) {
            41:             gst[i + len1] = word2.substring(i) + tail2;
            42:         }
            43:         Arrays.sort(gst);
            44:         for (int i = 0; i < gst.length - 1; i++) {
            45:             String str = getLCP(gst[i], gst[i + 1]);
            46:             if (str.length() > res.length()) {
            47:                 res = str;
            48:             }
            49:         }
            50:         if (res.endsWith("$"))
            51:             res = res.substring(0, res.length() - 1);
            52:         return res;
            53:     }
            54:  
            55:     private String mergeSuffix(String word1, String word2) {
            56:         StringBuffer sb = new StringBuffer();
            57:         sb.append(word1);
            58:         sb.append(tail1);
            59:         sb.append(word2);
            60:         sb.append(tail2);
            61:         return sb.toString();
            62:     }

          我的應(yīng)用是需要做字符串相似度的計算,所以需要求出來最長公共子串。在沒有任何語義背景的解釋的情況下,一個自己設(shè)計的相似度模型如下:

          clip_image002

          posted on 2011-06-03 10:18 changedi 閱讀(2602) 評論(0)  編輯  收藏 所屬分類: 算法

          主站蜘蛛池模板: 荔波县| 海南省| 滨州市| 吉木萨尔县| 福州市| 蒲城县| 灵丘县| 黔南| 滕州市| 于田县| 花莲市| 嘉黎县| 揭阳市| 乐平市| 克东县| 任丘市| 新密市| 嘉兴市| 蚌埠市| 家居| 齐齐哈尔市| 阳泉市| 涟源市| 会泽县| 河西区| 和平区| 湘阴县| 湘潭市| 罗平县| 小金县| 留坝县| 巴里| 竹溪县| 西乡县| 金川县| 衡东县| 绍兴市| 家居| 玉溪市| 富阳市| 阿拉善左旗|