-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path522+Longest Uncommon Subsequence II.cpp
More file actions
43 lines (35 loc) · 1.19 KB
/
522+Longest Uncommon Subsequence II.cpp
File metadata and controls
43 lines (35 loc) · 1.19 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
bool isSubsequence(string &s1, string &s2) {
int m = s1.size(), n = s2.size();
if (m > n) {
return false;
}
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i < m + 1; ++i) {
for (int j = 1; j < n + 1; ++j) {
if (s1[i-1] == s2[j-1]) {
dp[i][j] = dp[i-1][j-1] + 1;
} else {
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
return dp[m][n] == m; // m一定是最小的
}
int findLUSlength(vector<string>& strs) {
int res = -1;
for (int i = 0; i < strs.size(); ++i) {
bool flag = true;
for (int j = 0; j < strs.size(); ++j) {
if (i == j) continue;
if (isSubsequence(strs[i], strs[j])) { //如果它是其它字符串的子串
flag = false;
break;
}
}
if (flag) res = max(res, (int)strs[i].size());
}
return res;
}
};