-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest-string-chain.cpp
49 lines (46 loc) · 1.39 KB
/
longest-string-chain.cpp
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
44
45
46
47
48
49
class Solution {
public:
// 这道题目重点在于ju
int longestStrChain(vector<string>& words) {
if(words.size()==0) return 0;
auto cmp=[](string &a, string &b){
return a.size()<b.size();
};
sort(words.begin(),words.end(),cmp);
vector<int> vec(words.size(),1);
int res=1;
int k=0;
for(int i=0;i<words.size();i++){
for(int j=i-1;j>=0;j--){
if(words[i].size()-words[j].size()==1 && ju1(words[j],words[i])){
vec[i]=max(vec[i],vec[j]+1);
res=max(res,vec[i]);
}
}
}
return res;
}
bool ju(const string &a, const string &b){
if (a.size() + 1 != b.size())
return false;
int n = a.size();
for (int i = 0; i <= n; i++) // 每次忽略掉b[i]
if (a == b.substr(0, i) + b.substr(i + 1))
return true;
return false;
}
// 自己写的,参考别人的
bool ju1(const string &a, const string &b){
int la=a.size(),lb=b.size();
int i=0,j=0;
int count=0; // 不一致的个数
while(j<lb){
while(j<lb && i<la && a[i]==b[j]) i++,j++;
if(j==lb || i==la) return true;
count++;
j++;
if(count>1) return false;
}
return true;
}
};