forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1048.cpp
38 lines (36 loc) · 753 Bytes
/
1048.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
class Solution
{
public:
bool isPreString(string& s1, string& s2)
{
int p = 0, n = s1.size();
for (int i = 0; i < n; i++)
{
if (s1[i] != s2[i + p])
{
if (++p != 1) return false;
else --i;
}
}
return true;
}
int longestStrChain(vector<string>& words)
{
vector<pair<string, int>> mem[17];
for (auto& w : words) mem[w.size()].emplace_back(w, 1);
int res = 1;
for (int i = 2; i <= 16; i++)
{
for (auto& s2 : mem[i])
{
for (auto& s1 : mem[i - 1])
{
if (isPreString(s1.first, s2.first))
s2.second = max(s2.second, s1.second + 1);
}
res = max(res, s2.second);
}
}
return res;
}
};