国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Lintcode: Longest Common Substring

2019-11-14 23:10:58
字體:
來源:轉載
供稿:網(wǎng)友
Lintcode: Longest Common Substring
Given two strings, find the longest common substring.Return the length of it.NoteThe characters in substring should occur continiously in original string. This is different with subsequnce.ExampleGiven A=“ABCD”, B=“CBCE”, return  2
DP:D[i][j] 定義為:兩個string的前i個和前j個字符串,尾部連到最后的最長子串。D[i][j] =1. i = 0 || j = 0 : 02. s1.char[i - 1] = s2.char[j - 1] ? D[i-1][j-1] + 1 : 0;另外,創(chuàng)建一個max的緩存,不段更新即可。
 1 public class Solution { 2     /** 3      * @param A, B: Two strings. 4      * @return: The length of longest common subsequence of A and B. 5      */ 6     public int longestCommonSubstring(String A, String B) { 7         // write your code here 8         int[][] res = new int[A.length()+1][B.length()+1]; 9         int result = 0;10         for (int i=0; i<=A.length(); i++) {11             for (int j=0; j<=B.length(); j++) {12                 if (i==0 || j==0) {13                     res[i][j] = 0;14                     continue;15                 }16                 if (A.charAt(i-1) != B.charAt(j-1)) res[i][j] = 0;17                 else res[i][j] = res[i-1][j-1]+1;18                 result = Math.max(result, res[i][j]);19             }20         }21         return result;22     }23 }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 玉环县| 福泉市| 柘城县| 泗洪县| 红河县| 万宁市| 衡山县| 罗江县| 南陵县| 鲜城| 吉安县| 舒兰市| 皮山县| 莲花县| 思茅市| 新源县| 铁岭县| 阜宁县| 深州市| 洪湖市| 天祝| 廉江市| 城固县| 阿勒泰市| 西城区| 阳东县| 合肥市| 宜川县| 辽中县| 镇安县| 乌拉特后旗| 商洛市| 平阳县| 共和县| 达拉特旗| 固始县| 彩票| 梅州市| 高唐县| 汝城县| 蕲春县|