-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1048_longest_str_chain.cc
45 lines (41 loc) · 1.05 KB
/
1048_longest_str_chain.cc
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
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <tuple>
#include <queue>
#include <stack>
#include <unordered_map>
using namespace::std;
class Solution {
public:
static bool cmp(const string& s1, const string& s2) {
return s1.size() < s2.size();
}
int longestStrChain(vector<string>& words) {
int maxlen = 0;
sort(words.begin(), words.end(), cmp);
unordered_map<string, int> dict;
for (int i = 0; i < words.size(); ++i) {
int tmp = 0;
for (int j = 0; j < words[i].size(); ++j) {
string s;
for (int k = 0; k < words[i].size(); ++k) {
if (k != j)
s.push_back(words[i][k]);
}
if (dict.find(s) != dict.end())
tmp = max(tmp, dict[s]);
}
dict[words[i]] = tmp + 1;
maxlen = max(maxlen, tmp + 1);
}
return maxlen;
}
};
int main()
{
return 0;
}