-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1048.cpp
More file actions
46 lines (42 loc) · 1.11 KB
/
1048.cpp
File metadata and controls
46 lines (42 loc) · 1.11 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
44
45
46
class Solution {
public:
bool check(string& a, string& b){
if(a.size() + 1 != b.size())
return false;
int count[26];
memset(count,0,sizeof(count));
for(auto x: b)
++count[x-'a'];
for(auto x: a){
--count[x-'a'];
if(count[x-'a']<0)
return false;
}
int res = 0;
for(int i=0; i<26;i++){
if(count[i]!=0)
res += count[i];
}
return (res==1);
}
static bool compare(string &a, string &b){
if(a.size()<b.size())
return true;
return false;
}
int longestStrChain(vector<string>& words) {
int n = words.size();
sort(words.begin(),words.end(),compare);
vector<int> dp(n,1);
int res = 1;
for(int i=1; i<n; i++){
for(int j=0; j<i; j++){
if(check(words[j],words[i]) && dp[i] < dp[j]+1)
dp[i] = dp[j]+1;
}
if(res<dp[i])
res = dp[i];
}
return res;
}
};