LIS 和 LCS 子串行子串問題

2021-09-17 00:21:28 字數 1250 閱讀 1587

"abcdefg" : 子串行是可以不連續的 eg:"acg", 子串是必須連續的eg:"abc".

1) lc子串行(子串行是看某點(可不選)之前能組成的所有)

public int longestcommonsubsequence(string a, string b) else}}

return dp[m][n];

}

2) lc子串 substring (子串是看以某兩個點(必選)結尾的substring)

public int longestcommonsubstring(string a, string b) }}

return max;

}

1) o(n^2) 解法dp

public int lengthoflis(int nums) 

}max = math.max(dp[i], max);

}return max;

}

2) o(nlgn) 解法 binary search

public int lengthoflis(int nums) 

for(int i = res.length - 1; i >= 0; i--)

return 1;

}public int binarysearch(int arr, int num)

if(arr[left] >= num) return left;

return right;

}

1) substring 連續的

example:

input: "babad"

output: "bab"

note: "aba" is also a valid answer.

public class solution 

return s.substring(left, left + len);

}public void expand(string s, int i, int j)

if(j - i - 1 > len)

}}

2) subsequences

public class solution else}}

return dp[0][s.length()-1];

}}

最長公共子串行 LIS和LCS

題目描述 給出1 n的兩個排列p1和p2,求它們的最長公共子串行。輸入輸出格式 輸入格式 第一行是乙個數n,接下來兩行,每行為n個數,為自然數1 n的乙個排列。輸出格式 乙個數,即最長公共子串行的長度 輸入輸出樣例 輸入樣例 1 3 2 1 4 5 1 2 3 4 5 輸出樣例 13說明 資料規模 ...

LIS和LCS 最長上公升子串行和最長公共子串行

stl中關於二分查詢的函式有三個lower bound upper bound binary search 這三個函式都運用於有序區間 當然這也是運用二分查詢的前提 下面記錄一下這兩個函式。lower bound forwarditer first,forwarditer last,const tp...

上公升子串行 LIS

乙個只包含非負整數的序列bi,當b1 b2 bs的時候,我們稱這個序列是上公升的。對於給定的乙個序列,我們可以得到一些上公升的子串行,這裡1 i1 i2 ik n。例如 對於序列,有它的一些上公升子串行,如,等等。這些子串行中序列和最大的是子串行,它的所有元素的和為18。對於給定的乙個序列,求出它的...