Posted 娣卞叆鍘熺悊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鐐瑰嚮馃憜钃濊壊鈥?nbsp;娣卞叆鍘熺悊鈥濓紝鍏虫敞骞垛€?/span>璁句负鏄熸爣鈥?/span>
鎶€鏈共璐э紝绗竴鏃堕棿鎺ㄩ€?/span>
鍦?/span>Trie 鏍?span data-slate-object="text" data-key="1179">閭h妭鎴戜滑璁茶繃锛屽埄鐢?Trie 鏍戯紝鍙互瀹炵幇鎼滅储寮曟搸鐨勫叧閿瘝鎻愮ず鍔熻兘锛岃繖鏍峰彲浠ヨ妭鐪佺敤鎴疯緭鍏ユ悳绱㈠叧閿瘝鐨勬椂闂淬€傚疄闄呬笂锛屾悳绱㈠紩鎿庡湪鐢ㄦ埛浣撻獙鏂归潰鐨勪紭鍖栬繕鏈夊緢澶氾紝姣斿浣犲彲鑳界粡甯镐細鐢ㄧ殑鎷煎啓绾犻敊鍔熻兘銆?/span>
褰撲綘鍦ㄦ悳绱㈡涓紝涓€涓嶅皬蹇冭緭閿欏崟璇嶆椂锛屾悳绱㈠紩鎿庝細闈炲父鏅鸿兘鍦版娴嬪嚭浣犵殑鎷煎啓閿欒锛屽苟涓旂敤瀵瑰簲鐨勬纭崟璇嶆潵杩涜鎼滅储銆備綔涓轰竴鍚嶈蒋浠跺紑鍙戝伐绋嬪笀锛屼綘鏄惁鎯宠繃锛岃繖涓姛鑳芥槸鎬庝箞瀹炵幇鐨勫憿锛?/span>
濡備綍閲忓寲涓や釜瀛楃涓茬殑鐩镐技搴︼紵
璁$畻鏈哄彧璁よ瘑鏁板瓧锛屾墍浠ヨ瑙g瓟寮€绡囩殑闂锛屾垜浠氨瑕佸厛鏉ョ湅锛屽浣曢噺鍖栦袱涓瓧绗︿覆涔嬮棿鐨勭浉浼肩▼搴﹀憿锛熸湁涓€涓潪甯歌憲鍚嶇殑閲忓寲鏂规硶锛岄偅灏辨槸缂栬緫璺濈锛圗dit Distance锛夈€?/span>
椤惧悕鎬濅箟锛?/span>缂栬緫璺濈鎸囩殑灏辨槸锛屽皢涓€涓瓧绗︿覆杞寲鎴愬彟涓€涓瓧绗︿覆锛岄渶瑕佺殑鏈€灏戠紪杈戞搷浣滄鏁帮紙姣斿澧炲姞涓€涓瓧绗︺€佸垹闄や竴涓瓧绗︺€佹浛鎹竴涓瓧绗︼級銆傜紪杈戣窛绂昏秺澶э紝璇存槑涓や釜瀛楃涓茬殑鐩镐技绋嬪害瓒婂皬锛涚浉鍙嶏紝缂栬緫璺濈灏辫秺灏忥紝璇存槑涓や釜瀛楃涓茬殑鐩镐技绋嬪害瓒婂ぇ銆傚浜庝袱涓畬鍏ㄧ浉鍚岀殑瀛楃涓叉潵璇达紝缂栬緫璺濈灏辨槸 0銆?/span>
鏍规嵁鎵€鍖呭惈鐨勭紪杈戞搷浣滅绫荤殑涓嶅悓锛岀紪杈戣窛绂绘湁澶氱涓嶅悓鐨勮绠楁柟寮忥紝姣旇緝钁楀悕鐨勬湁鑾辨枃鏂潶璺濈锛圠evenshtein distance锛夊拰鏈€闀垮叕鍏卞瓙涓查暱搴?/span>锛圠ongest common substring length锛夈€傚叾涓紝鑾辨枃鏂潶璺濈鍏佽澧炲姞銆佸垹闄ゃ€佹浛鎹㈠瓧绗﹁繖涓変釜缂栬緫鎿嶄綔锛屾渶闀垮叕鍏卞瓙涓查暱搴﹀彧鍏佽澧炲姞銆佸垹闄ゅ瓧绗﹁繖涓や釜缂栬緫鎿嶄綔銆?/span>
鑰屼笖锛岃幈鏂囨柉鍧﹁窛绂诲拰鏈€闀垮叕鍏卞瓙涓查暱搴︼紝浠庝袱涓埅鐒剁浉鍙嶇殑瑙掑害锛屽垎鏋愬瓧绗︿覆鐨勭浉浼肩▼搴︺€傝幈鏂囨柉鍧﹁窛绂荤殑澶у皬锛岃〃绀轰袱涓瓧绗︿覆宸紓鐨勫ぇ灏忥紱鑰屾渶闀垮叕鍏卞瓙涓茬殑澶у皬锛岃〃绀轰袱涓瓧绗︿覆鐩镐技绋嬪害鐨勫ぇ灏忋€?/span>
鍏充簬杩欎袱涓绠楁柟娉曪紝鎴戜妇涓緥瀛愮粰浣犺鏄庝竴涓嬨€傝繖閲岄潰锛屼袱涓瓧绗︿覆 mitcmu 鍜?mtacnu 鐨勮幈鏂囨柉鍧﹁窛绂绘槸 3锛屾渶闀垮叕鍏卞瓙涓查暱搴︽槸 4銆?/span>
浜嗚В浜嗙紪杈戣窛绂荤殑姒傚康涔嬪悗锛屾垜浠潵鐪嬶紝濡備綍蹇€熻绠椾袱涓瓧绗︿覆涔嬮棿鐨勭紪杈戣窛绂伙紵
濡備綍缂栫▼璁$畻鑾辨枃鏂潶璺濈锛?/span>
涔嬪墠鎴戝弽澶嶅己璋冭繃锛屾€濊€冭繃绋嬫瘮缁撹鏇撮噸瑕侊紝鎵€浠ワ紝鎴戠幇鍦ㄥ氨缁欎綘灞曠ず涓€涓嬶紝瑙e喅杩欎釜闂锛屾垜鐨勫畬鏁寸殑鎬濊€冭繃绋嬨€?/span>
杩欎釜闂鏄眰鎶婁竴涓瓧绗︿覆鍙樻垚鍙︿竴涓瓧绗︿覆锛岄渶瑕佺殑鏈€灏戠紪杈戞鏁般€傛暣涓眰瑙h繃绋嬶紝娑夊強澶氫釜鍐崇瓥闃舵锛屾垜浠渶瑕佷緷娆¤€冨療涓€涓瓧绗︿覆涓殑姣忎釜瀛楃锛岃窡鍙︿竴涓瓧绗︿覆涓殑瀛楃鏄惁鍖归厤锛屽尮閰嶇殑璇濆浣曞鐞嗭紝涓嶅尮閰嶇殑璇濆張濡備綍澶勭悊銆傛墍浠ワ紝杩欎釜闂绗﹀悎澶氶樁娈靛喅绛栨渶浼樿В妯″瀷銆?/span>
鎴戜滑鍓嶉潰璁蹭簡锛岃椽蹇冦€佸洖婧€佸姩鎬佽鍒掑彲浠ヨВ鍐崇殑闂锛岄兘鍙互鎶借薄鎴愯繖鏍蜂竴涓ā鍨嬨€傝瑙e喅杩欎釜闂锛屾垜浠彲浠ュ厛鐪嬩竴鐪嬶紝鐢ㄦ渶绠€鍗曠殑鍥炴函绠楁硶锛岃濡備綍鏉ヨВ鍐炽€?/span>
鍥炴函鏄竴涓€掑綊澶勭悊鐨勮繃绋嬨€傚鏋?a[i]涓?b[j]鍖归厤锛屾垜浠€掑綊鑰冨療 a[i+1]鍜?b[j+1]銆傚鏋?a[i]涓?b[j]涓嶅尮閰嶏紝閭f垜浠湁澶氱澶勭悊鏂瑰紡鍙€夛細
鍙互鍒犻櫎 a[i]锛岀劧鍚庨€掑綊鑰冨療 a[i+1]鍜?b[j]锛?/span>
鍙互鍒犻櫎 b[j]锛岀劧鍚庨€掑綊鑰冨療 a[i]鍜?b[j+1]锛?/span>
鍙互鍦?a[i]鍓嶉潰娣诲姞涓€涓窡 b[j]鐩稿悓鐨勫瓧绗︼紝鐒跺悗閫掑綊鑰冨療 a[i]鍜?b[j+1];
鍙互鍦?b[j]鍓嶉潰娣诲姞涓€涓窡 a[i]鐩稿悓鐨勫瓧绗︼紝鐒跺悗閫掑綊鑰冨療 a[i+1]鍜?b[j]锛?/span>
鍙互灏?a[i]鏇挎崲鎴?b[j]锛屾垨鑰呭皢 b[j]鏇挎崲鎴?a[i]锛岀劧鍚庨€掑綊鑰冨療 a[i+1]鍜?b[j+1]銆?/span>
鎴戜滑灏嗕笂闈㈢殑鍥炴函绠楁硶鐨勫鐞嗘€濊矾锛岀炕璇戞垚浠g爜锛屽氨鏄笅闈㈣繖涓牱瀛愶細
private char[] a = "mitcmu".toCharArray();
private char[] b = "mtacnu".toCharArray();
private int n = 6;
private int m = 6;
private int minDist = Integer.MAX_VALUE; // 瀛樺偍缁撴灉
// 璋冪敤鏂瑰紡 lwstBT(0, 0, 0);
public lwstBT(int i, int j, int edist) {
if (i == n || j == m) {
if (i < n) edist += (n-i);
if (j < m) edist += (m - j);
if (edist < minDist) minDist = edist;
return;
}
if (a[i] == b[j]) { // 涓や釜瀛楃鍖归厤
lwstBT(i+1, j+1, edist);
} else { // 涓や釜瀛楃涓嶅尮閰?/span>
lwstBT(i + 1, j, edist + 1); // 鍒犻櫎a[i]鎴栬€卋[j]鍓嶆坊鍔犱竴涓瓧绗?/span>
lwstBT(i, j + 1, edist + 1); // 鍒犻櫎b[j]鎴栬€卆[i]鍓嶆坊鍔犱竴涓瓧绗?/span>
lwstBT(i + 1, j + 1, edist + 1); // 灏哸[i]鍜宐[j]鏇挎崲涓虹浉鍚屽瓧绗?/span>
}
}
鏍规嵁鍥炴函绠楁硶鐨勪唬鐮佸疄鐜帮紝鎴戜滑鍙互鐢诲嚭閫掑綊鏍戯紝鐪嬫槸鍚﹀瓨鍦ㄩ噸澶嶅瓙闂銆傚鏋滃瓨鍦ㄩ噸澶嶅瓙闂锛岄偅鎴戜滑灏卞彲浠ヨ€冭檻鑳藉惁鐢ㄥ姩鎬佽鍒掓潵瑙e喅锛涘鏋滀笉瀛樺湪閲嶅瀛愰棶棰橈紝閭e洖婧氨鏄渶濂界殑瑙e喅鏂规硶銆?/span>
鍦ㄩ€掑綊鏍戜腑锛屾瘡涓妭鐐逛唬琛ㄤ竴涓姸鎬侊紝鐘舵€佸寘鍚笁涓彉閲?(i, j, edist)锛屽叾涓紝edist 琛ㄧず澶勭悊鍒?a[i]鍜?b[j]鏃讹紝宸茬粡鎵ц鐨勭紪杈戞搷浣滅殑娆℃暟銆?/span>
鍦ㄩ€掑綊鏍戜腑锛?i, j) 涓や釜鍙橀噺閲嶅鐨勮妭鐐瑰緢澶氾紝姣斿 (3, 2) 鍜?(2, 3)銆傚浜?(i, j) 鐩稿悓鐨勮妭鐐癸紝鎴戜滑鍙渶瑕佷繚鐣?edist 鏈€灏忕殑锛岀户缁€掑綊澶勭悊灏卞彲浠ヤ簡锛屽墿涓嬬殑鑺傜偣閮藉彲浠ヨ垗寮冦€傛墍浠ワ紝鐘舵€佸氨浠?(i, j, edist) 鍙樻垚浜?(i, j, min_edist)锛屽叾涓?min_edist 琛ㄧず澶勭悊鍒?a[i]鍜?b[j]锛屽凡缁忔墽琛岀殑鏈€灏戠紪杈戞鏁般€?/span>
鐪嬪埌杩欓噷锛屼綘鏈夋病鏈夎寰楋紝杩欎釜闂璺熶笂涓よ妭璁茬殑鍔ㄦ€佽鍒掍緥瀛愰潪甯哥浉浼硷紵涓嶈繃锛岃繖涓棶棰樼殑鐘舵€佽浆绉绘柟寮忥紝瑕佹瘮涔嬪墠涓よ妭璇句腑璁插埌鐨勪緥瀛愰兘瑕佸鏉傚緢澶氥€備笂涓€鑺傛垜浠鐨勭煩闃垫渶鐭矾寰勯棶棰樹腑锛屽埌杈剧姸鎬?(i, j) 鍙兘閫氳繃 (i-1, j) 鎴?(i, j-1) 涓や釜鐘舵€佽浆绉昏繃鏉ワ紝鑰屼粖澶╄繖涓棶棰橈紝鐘舵€?(i, j) 鍙兘浠?(i-1, j)锛?i, j-1)锛?i-1, j-1) 涓変釜鐘舵€佷腑鐨勪换鎰忎竴涓浆绉昏繃鏉ャ€?/span>
鍩轰簬鍒氬垰鐨勫垎鏋愶紝鎴戜滑鍙互灏濊瘯鐫€灏嗘妸鐘舵€佽浆绉荤殑杩囩▼锛岀敤鍏紡鍐欏嚭鏉ャ€傝繖灏辨槸鎴戜滑鍓嶉潰璁茬殑鐘舵€佽浆绉绘柟绋嬨€?/span>
濡傛灉锛歛[i]!=b[j]锛岄偅涔堬細min_edist(i, j)灏辩瓑浜庯細
min(min_edist(i-1,j)+1, min_edist(i,j-1)+1, min_edist(i-1,j-1)+1)
濡傛灉锛歛[i]==b[j]锛岄偅涔堬細min_edist(i, j)灏辩瓑浜庯細
min(min_edist(i-1,j)+1, min_edist(i,j-1)+1锛宮in_edist(i-1,j-1))
鍏朵腑锛宮in琛ㄧず姹備笁鏁颁腑鐨勬渶灏忓€笺€?nbsp;
浜嗚В浜嗙姸鎬佷笌鐘舵€佷箣闂寸殑閫掓帹鍏崇郴锛屾垜浠敾鍑轰竴涓簩缁寸殑鐘舵€佽〃锛屾寜琛屼緷娆℃潵濉厖鐘舵€佽〃涓殑姣忎釜鍊笺€?/span>
鎴戜滑鐜板湪鏃㈡湁鐘舵€佽浆绉绘柟绋嬶紝鍙堢悊娓呬簡瀹屾暣鐨勫~琛ㄨ繃绋嬶紝浠g爜瀹炵幇灏遍潪甯哥畝鍗曚簡銆傛垜灏嗕唬鐮佽创鍦ㄤ笅闈紝浣犲彲浠ュ姣旂潃鏂囧瓧瑙i噴锛屼竴璧风湅涓嬨€?/span>
public int lwstDP(char[] a, int n, char[] b, int m) {
int[][] minDist = new int[n][m];
for (int j = 0; j < m; ++j) { // 鍒濆鍖栫0琛?a[0..0]涓巄[0..j]鐨勭紪杈戣窛绂?/span>
if (a[0] == b[j]) minDist[0][j] = j;
else if (j != 0) minDist[0][j] = minDist[0][j-1]+1;
else minDist[0][j] = 1;
}
for (int i = 0; i < n; ++i) { // 鍒濆鍖栫0鍒?a[0..i]涓巄[0..0]鐨勭紪杈戣窛绂?/span>
if (a[i] == b[0]) minDist[i][0] = i;
else if (i != 0) minDist[i][0] = minDist[i-1][0]+1;
else minDist[i][0] = 1;
}
for (int i = 1; i < n; ++i) { // 鎸夎濉〃
for (int j = 1; j < m; ++j) {
if (a[i] == b[j]) minDist[i][j] = min(
minDist[i-1][j]+1, minDist[i][j-1]+1, minDist[i-1][j-1]);
else minDist[i][j] = min(
minDist[i-1][j]+1, minDist[i][j-1]+1, minDist[i-1][j-1]+1);
}
}
return minDist[n-1][m-1];
}
private int min(int x, int y, int z) {
int minv = Integer.MAX_VALUE;
if (x < minv) minv = x;
if (y < minv) minv = y;
if (z < minv) minv = z;
return minv;
}
浣犲彲鑳戒細璇达紝鎴戣櫧鐒惰兘鐪嬫噦浣犺鐨勬€濊矾锛屼絾鏄亣鍒版柊鐨勯棶棰樼殑鏃跺€欙紝鎴戣繕鏄細鎰熻鍒版棤浠庝笅鎵嬨€傝繖绉嶆劅瑙夋槸闈炲父姝e父鐨勩€傚叧浜庡鏉傜畻娉曢棶棰樼殑瑙e喅鎬濊矾锛屾垜杩樻湁涓€浜涚粡楠屻€佸皬鎶€宸э紝鍙互鍒嗕韩缁欎綘銆?/span>
褰撴垜浠嬁鍒颁竴涓棶棰樼殑鏃跺€欙紝鎴戜滑鍙互鍏堜笉鎬濊€冿紝璁$畻鏈轰細濡備綍瀹炵幇杩欎釜闂锛岃€屾槸鍗曠函鑰冭檻鈥滀汉鑴戔€濅細濡備綍鍘昏В鍐宠繖涓棶棰?/span>銆備汉鑴戞瘮杈冨€惧悜浜庢€濊€冨叿璞″寲鐨勩€佹懜寰楃潃鐪嬪緱瑙佺殑涓滆タ锛屼笉閫傚悎鎬濊€冭繃浜庢娊璞$殑闂銆傛墍浠ワ紝鎴戜滑闇€瑕佹妸鎶借薄闂鍏疯薄鍖栥€傞偅濡備綍鍏疯薄鍖栧憿锛熸垜浠彲浠ュ疄渚嬪寲鍑犱釜娴嬭瘯鏁版嵁锛岄€氳繃浜鸿剳鍘诲垎鏋愬叿浣撳疄渚嬬殑瑙o紝鐒跺悗鎬荤粨瑙勫緥锛屽啀灏濊瘯濂楃敤瀛﹁繃鐨勭畻娉曪紝鐪嬫槸鍚﹁兘澶熻В鍐炽€?/span>
闄ゆ涔嬪锛屾垜杩樻湁涓€涓潪甯告湁鏁堛€佷絾涔熺畻涓嶄笂鎶€宸х殑涓滆タ锛屾垜涔熷弽澶嶅己璋冭繃锛岄偅灏辨槸澶氱粌銆傚疄闄呬笂锛岀瓑浣犲仛澶氫簡棰樼洰涔嬪悗锛岃嚜鐒跺氨浼氭湁鎰熻锛岀湅鍒伴棶棰橈紝绔嬮┈灏辫兘鎯冲埌鑳藉惁鐢ㄥ姩鎬佽鍒掕В鍐筹紝鐒跺悗鐩存帴灏卞彲浠ュ鎵炬渶浼樺瓙缁撴瀯锛屽啓鍑哄姩鎬佽鍒掓柟绋嬶紝鐒跺悗灏嗙姸鎬佽浆绉绘柟绋嬬炕璇戞垚浠g爜銆?/span>
濡備綍缂栫▼璁$畻鏈€闀垮叕鍏卞瓙涓查暱搴︼紵
鍓嶉潰鎴戜滑璁插埌锛屾渶闀垮叕鍏卞瓙涓蹭綔涓虹紪杈戣窛绂讳腑鐨勪竴绉嶏紝鍙厑璁稿鍔犮€佸垹闄ゅ瓧绗︿袱绉嶇紪杈戞搷浣溿€備粠鍚嶅瓧涓婏紝浣犲彲鑳借寰楀畠鐪嬭捣鏉ヨ窡缂栬緫璺濈娌′粈涔堝叧绯汇€傚疄闄呬笂锛屼粠鏈川涓婃潵璇达紝瀹冭〃寰佺殑涔熸槸涓や釜瀛楃涓蹭箣闂寸殑鐩镐技绋嬪害銆?/span>
杩欎釜闂鐨勮В鍐虫€濊矾锛岃窡鑾辨枃鏂潶璺濈鐨勮В鍐虫€濊矾闈炲父鐩镐技锛屼篃鍙互鐢ㄥ姩鎬佽鍒掕В鍐炽€傛垜鍒氬垰宸茬粡璇︾粏璁茶В浜嗚幈鏂囨柉鍧﹁窛绂荤殑鍔ㄦ€佽鍒掕В鍐虫€濊矾锛屾墍浠ワ紝閽堝杩欎釜闂锛屾垜鐩存帴瀹氫箟鐘舵€侊紝鐒跺悗鍐欑姸鎬佽浆绉绘柟绋嬨€?/span>
姣忎釜鐘舵€佽繕鏄寘鎷笁涓彉閲?(i, j, max_lcs)锛宮ax_lcs 琛ㄧず a[0鈥]鍜?b[0鈥]鐨勬渶闀垮叕鍏卞瓙涓查暱搴︺€傞偅 (i, j) 杩欎釜鐘舵€侀兘鏄敱鍝簺鐘舵€佽浆绉昏繃鏉ョ殑鍛紵
鎴戜滑鍏堟潵鐪嬪洖婧殑澶勭悊鎬濊矾銆傛垜浠粠 a[0]鍜?b[0]寮€濮嬶紝渚濇鑰冨療涓や釜瀛楃涓蹭腑鐨勫瓧绗︽槸鍚﹀尮閰嶃€?/span>
濡傛灉 a[i]涓?b[j]浜掔浉鍖归厤锛屾垜浠皢鏈€澶у叕鍏卞瓙涓查暱搴﹀姞涓€锛屽苟涓旂户缁€冨療 a[i+1]鍜?b[j+1]銆?/span>
濡傛灉 a[i]涓?b[j]涓嶅尮閰嶏紝鏈€闀垮叕鍏卞瓙涓查暱搴︿笉鍙橈紝杩欎釜鏃跺€欙紝鏈変袱涓笉鍚岀殑鍐崇瓥璺嚎锛?/span>
鍒犻櫎 a[i]锛屾垨鑰呭湪 b[j]鍓嶉潰鍔犱笂涓€涓瓧绗?a[i]锛岀劧鍚庣户缁€冨療 a[i+1]鍜?b[j]锛?/span>
鍒犻櫎 b[j]锛屾垨鑰呭湪 a[i]鍓嶉潰鍔犱笂涓€涓瓧绗?b[j]锛岀劧鍚庣户缁€冨療 a[i]鍜?b[j+1]銆?/span>
鍙嶈繃鏉ヤ篃灏辨槸璇达紝濡傛灉鎴戜滑瑕佹眰 a[0鈥]鍜?b[0鈥]鐨勬渶闀垮叕鍏遍暱搴?max_lcs(i, j)锛屾垜浠彧鏈夊彲鑳介€氳繃涓嬮潰涓変釜鐘舵€佽浆绉昏繃鏉ワ細
(i-1, j-1, max_lcs)锛屽叾涓?max_lcs 琛ㄧず a[0鈥-1]鍜?b[0鈥-1]鐨勬渶闀垮叕鍏卞瓙涓查暱搴︼紱
(i-1, j, max_lcs)锛屽叾涓?max_lcs 琛ㄧず a[0鈥-1]鍜?b[0鈥]鐨勬渶闀垮叕鍏卞瓙涓查暱搴︼紱
(i, j-1, max_lcs)锛屽叾涓?max_lcs 琛ㄧず a[0鈥]鍜?b[0鈥-1]鐨勬渶闀垮叕鍏卞瓙涓查暱搴︺€?/span>
濡傛灉鎴戜滑鎶婅繖涓浆绉昏繃绋嬶紝鐢ㄧ姸鎬佽浆绉绘柟绋嬪啓鍑烘潵锛屽氨鏄笅闈㈣繖涓牱瀛愶細
濡傛灉锛歛[i]==b[j]锛岄偅涔堬細max_lcs(i, j)灏辩瓑浜庯細
max(max_lcs(i-1,j-1)+1, max_lcs(i-1, j), max_lcs(i, j-1))锛?/span>
濡傛灉锛歛[i]!=b[j]锛岄偅涔堬細max_lcs(i, j)灏辩瓑浜庯細
max(max_lcs(i-1,j-1), max_lcs(i-1, j), max_lcs(i, j-1))锛?/span>
鍏朵腑max琛ㄧず姹備笁鏁颁腑鐨勬渶澶у€笺€?/span>
鏈変簡鐘舵€佽浆绉绘柟绋嬶紝浠g爜瀹炵幇灏辩畝鍗曞浜嗐€傛垜鎶婁唬鐮佽创鍒颁簡涓嬮潰锛屼綘鍙互瀵规瘮鐫€鏂囧瓧涓€鍧楀効鐪嬨€?/span>
public int lcs(char[] a, int n, char[] b, int m) {
int[][] maxlcs = new int[n][m];
for (int j = 0; j < m; ++j) {//鍒濆鍖栫0琛岋細a[0..0]涓巄[0..j]鐨刴axlcs
if (a[0] == b[j]) maxlcs[0][j] = 1;
else if (j != 0) maxlcs[0][j] = maxlcs[0][j-1];
else maxlcs[0][j] = 0;
}
for (int i = 0; i < n; ++i) {//鍒濆鍖栫0鍒楋細a[0..i]涓巄[0..0]鐨刴axlcs
if (a[i] == b[0]) maxlcs[i][0] = 1;
else if (i != 0) maxlcs[i][0] = maxlcs[i-1][0];
else maxlcs[i][0] = 0;
}
for (int i = 1; i < n; ++i) { // 濉〃
for (int j = 1; j < m; ++j) {
if (a[i] == b[j]) maxlcs[i][j] = max(
maxlcs[i-1][j], maxlcs[i][j-1], maxlcs[i-1][j-1]+1);
else maxlcs[i][j] = max(
maxlcs[i-1][j], maxlcs[i][j-1], maxlcs[i-1][j-1]);
}
}
return maxlcs[n-1][m-1];
}
private int max(int x, int y, int z) {
int maxv = Integer.MIN_VALUE;
if (x > maxv) maxv = x;
if (y > maxv) maxv = y;
if (z > maxv) maxv = z;
return maxv;
}
瑙g瓟寮€绡?/span>
浠婂ぉ鐨勫唴瀹瑰埌姝ゅ氨璁插畬浜嗭紝鎴戜滑鏉ョ湅涓嬪紑绡囩殑闂銆?/span>
褰撶敤鎴峰湪鎼滅储妗嗗唴锛岃緭鍏ヤ竴涓嫾鍐欓敊璇殑鍗曡瘝鏃讹紝鎴戜滑灏辨嬁杩欎釜鍗曡瘝璺熻瘝搴撲腑鐨勫崟璇嶄竴涓€杩涜姣旇緝锛岃绠楃紪杈戣窛绂伙紝灏嗙紪杈戣窛绂绘渶灏忕殑鍗曡瘝锛屼綔涓虹籂姝d箣鍚庣殑鍗曡瘝锛屾彁绀虹粰鐢ㄦ埛銆?/span>
杩欏氨鏄嫾鍐欑籂閿欐渶鍩烘湰鐨勫師鐞嗐€備笉杩囷紝鐪熸鐢ㄤ簬鍟嗙敤鐨勬悳绱㈠紩鎿庯紝鎷煎啓绾犻敊鍔熻兘鏄剧劧涓嶄細灏辫繖涔堢畝鍗曘€備竴鏂归潰锛屽崟绾埄鐢ㄧ紪杈戣窛绂绘潵绾犻敊锛屾晥鏋滃苟涓嶄竴瀹氬ソ锛涘彟涓€鏂归潰锛岃瘝搴撲腑鐨勬暟鎹噺鍙兘寰堝ぇ锛屾悳绱㈠紩鎿庢瘡澶╄鏀寔娴烽噺鐨勬悳绱紝鎵€浠ュ绾犻敊鐨勬€ц兘瑕佹眰寰堥珮銆?/span>
閽堝绾犻敊鏁堟灉涓嶅ソ鐨勯棶棰橈紝鎴戜滑鏈夊緢澶氱浼樺寲鎬濊矾锛屾垜杩欓噷浠嬬粛鍑犵銆?/span>
鎴戜滑骞朵笉浠呬粎鍙栧嚭缂栬緫璺濈鏈€灏忕殑閭d釜鍗曡瘝锛岃€屾槸鍙栧嚭缂栬緫璺濈鏈€灏忕殑 TOP 10锛岀劧鍚庢牴鎹叾浠栧弬鏁帮紝鍐崇瓥閫夋嫨鍝釜鍗曡瘝浣滀负鎷煎啓绾犻敊鍗曡瘝銆傛瘮濡備娇鐢ㄦ悳绱㈢儹闂ㄧ▼搴︽潵鍐冲畾鍝釜鍗曡瘝浣滀负鎷煎啓绾犻敊鍗曡瘝銆?/span>
鎴戜滑杩樺彲浠ョ敤澶氱缂栬緫璺濈璁$畻鏂规硶锛屾瘮濡備粖澶╄鍒扮殑涓ょ锛岀劧鍚庡垎鍒紪杈戣窛绂绘渶灏忕殑 TOP 10锛岀劧鍚庢眰浜ら泦锛岀敤浜ら泦鐨勭粨鏋滐紝鍐嶇户缁紭鍖栧鐞嗐€?/span>
鎴戜滑杩樺彲浠ラ€氳繃缁熻鐢ㄦ埛鐨勬悳绱㈡棩蹇楋紝寰楀埌鏈€甯歌鎷奸敊鐨勫崟璇嶅垪琛紝浠ュ強瀵瑰簲鐨勬嫾鍐欐纭殑鍗曡瘝銆傛悳绱㈠紩鎿庡湪鎷煎啓绾犻敊鐨勬椂鍊欙紝棣栧厛鍦ㄨ繖涓渶甯歌鎷奸敊鍗曡瘝鍒楄〃涓煡鎵俱€傚鏋滀竴鏃︽壘鍒帮紝鐩存帴杩斿洖瀵瑰簲鐨勬纭殑鍗曡瘝銆傝繖鏍风籂閿欑殑鏁堟灉闈炲父濂姐€?/span>
鎴戜滑杩樻湁鏇村姞楂樼骇涓€鐐圭殑鍋氭硶锛屽紩鍏ヤ釜鎬у寲鍥犵礌銆傞拡瀵规瘡涓敤鎴凤紝缁存姢杩欎釜鐢ㄦ埛鐗规湁鐨勬悳绱㈠枩濂斤紝涔熷氨鏄父鐢ㄧ殑鎼滅储鍏抽敭璇嶃€傚綋鐢ㄦ埛杈撳叆閿欒鐨勫崟璇嶇殑鏃跺€欙紝鎴戜滑棣栧厛鍦ㄨ繖涓敤鎴峰父鐢ㄧ殑鎼滅储鍏抽敭璇嶄腑锛岃绠楃紪杈戣窛绂伙紝鏌ユ壘缂栬緫璺濈鏈€灏忕殑鍗曡瘝銆?/span>
閽堝绾犻敊鎬ц兘鏂归潰锛屾垜浠篃鏈夌浉搴旂殑浼樺寲鏂瑰紡銆傛垜璁蹭袱绉嶅垎娌荤殑浼樺寲鎬濊矾銆?/span>
濡傛灉绾犻敊鍔熻兘鐨?TPS 涓嶉珮锛屾垜浠彲浠ラ儴缃插鍙版満鍣紝姣忓彴鏈哄櫒杩愯涓€涓嫭绔嬬殑绾犻敊鍔熻兘銆傚綋鏈変竴涓籂閿欒姹傜殑鏃跺€欙紝鎴戜滑閫氳繃璐熻浇鍧囪 锛屽垎閰嶅埌鍏朵腑涓€鍙版満鍣紝鏉ヨ绠楃紪杈戣窛绂伙紝寰楀埌绾犻敊鍗曡瘝銆?/span>
濡傛灉绾犻敊绯荤粺鐨勫搷搴旀椂闂村お闀匡紝涔熷氨鏄紝姣忎釜绾犻敊璇锋眰澶勭悊鏃堕棿杩囬暱锛屾垜浠彲浠ュ皢绾犻敊鐨勮瘝搴擄紝鍒嗗壊鍒板緢澶氬彴鏈哄櫒銆傚綋鏈変竴涓籂閿欒姹傜殑鏃跺€欙紝鎴戜滑灏卞皢杩欎釜鎷煎啓閿欒鐨勫崟璇嶏紝鍚屾椂鍙戦€佸埌杩欏鍙版満鍣紝璁╁鍙版満鍣ㄥ苟琛屽鐞嗭紝鍒嗗埆寰楀埌缂栬緫璺濈鏈€灏忕殑鍗曡瘝锛岀劧鍚庡啀姣斿鍚堝苟锛屾渶缁堝喅瀹氬嚭涓€涓渶浼樼殑绾犻敊鍗曡瘝銆?/span>
鐪熸鐨勬悳绱㈠紩鎿庣殑鎷煎啓绾犻敊浼樺寲锛岃偗瀹氫笉姝㈡垜璁茬殑杩欎箞绠€鍗曪紝浣嗘槸涓囧彉涓嶇鍏跺畻銆傛帉鎻′簡鏍稿績鍘熺悊锛屽氨鏄帉鎻′簡瑙e喅闂鐨勬柟娉曪紝鍓╀笅灏遍潬浣犺嚜宸辩殑鐏垫椿杩愮敤鍜屽疄鎴樻搷缁冧簡銆?/span>
鍐呭灏忕粨
鍔ㄦ€佽鍒掔殑涓夎妭鍐呭鍒版灏卞叏閮ㄨ瀹屼簡锛屼笉鐭ラ亾浣犳帉鎻″緱濡備綍鍛紵
鍔ㄦ€佽鍒掔殑鐞嗚灏界骞朵笉澶嶆潅锛屾€荤粨璧锋潵灏辨槸鈥滀竴涓ā鍨嬩笁涓壒寰佲€濄€備絾鏄紝瑕佹兂鐏垫椿搴旂敤骞朵笉绠€鍗曘€傝鎯宠兘鐪熸鐞嗚В銆佹帉鎻″姩鎬佽鍒掞紝浣犲彧鏈夊缁冧範銆?/span>
杩欎笁鑺備腑锛屽姞涓婅鍚庢€濊€冮锛屾€诲叡鏈?8 涓姩鎬佽鍒掗棶棰樸€傝繖 8 涓棶棰橀兘闈炲父缁忓吀锛屾槸鎴戠簿蹇冪瓫閫夊嚭鏉ョ殑銆傚緢澶氬姩鎬佽鍒掗棶棰樺叾瀹為兘鍙互鎶借薄鎴愯繖鍑犱釜闂妯″瀷锛屾墍浠ワ紝浣犱竴瀹氳澶氱湅鍑犻亶锛屽鎬濊€冧竴涓嬶紝浜夊彇鐪熸鎼炴噦瀹冧滑銆?/span>
鍙寮勬噦浜嗚繖鍑犱釜闂锛屼竴鑸殑鍔ㄦ€佽鍒掗棶棰橈紝浣犲簲璇ラ兘鍙互搴斾粯銆傚浜庡姩鎬佽鍒掕繖涓煡璇嗙偣锛屼綘灏辩畻鏄叆闂ㄤ簡锛屽啀瀛︿範鏇村姞澶嶆潅鐨勫氨浼氱畝鍗曞緢澶氥€?/span>
-娣卞叆鍘熺悊-
鐭ュ叾鐒跺苟鐭ュ叾鎵€浠ョ劧
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章