-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode第1143题.cpp
More file actions
26 lines (26 loc) · 967 Bytes
/
Leetcode第1143题.cpp
File metadata and controls
26 lines (26 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
vector<vector<int>> dp(text1.size()+1,vector<int>(text2.size()+1,0));
//dp数组中,dp[i][j]表示到text1[i]和text2[j]之前最长的公共子数组的长度
int result=0;
for(int i=1;i<dp.size();++i)
{
for(int j=1;j<dp[0].size();++j)
{
if(text1[i-1]==text2[j-1]){
//如果当前的字符相同,那么当前最长的长度就等于上一个最大长度+1
dp[i][j]=dp[i-1][j-1]+1;
}
else{
//如果当前字符不相同,那么当前最长的长度就等于上一个最长长度
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
if(dp[i][j]>result){
result=dp[i][j];
}
}
}
return result;
}
};