class Solution {
public:
static bool cmp(const string& x,const string& y){
if(x.length() < y.length()) return true;
return false;
}
bool judge(const string& x,const string& y){
if(x.length() + 1 == y.length()){
int loc = 0;
int cnt = 0;
for(int i=0;i<y.length();i++){
if(x[loc] == y[i]){ loc++;continue;
}else cnt++;
}
if(loc == x.length() && cnt == 1) return true;
}
return false;
}
int longestStrChain(vector<string>& words) {
int N = words.size();
int dp[N+1];
memset(dp,0,sizeof(dp));
sort(words.begin(),words.end(),cmp);
int ans = 1;
for(int i=0;i<N;i++){
for(int j=0;j<i;j++){
if(judge(words[j],words[i])){
dp[i] = max(dp[i],dp[j]+1);
ans = max(dp[i]+1,ans);
}
}
}
return ans;
}
};
No.68 - LeetCode1048
最新推荐文章于 2023-04-27 22:06:21 发布