-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1048.cpp
89 lines (76 loc) · 1.85 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
inline bool isLinked(string a, string b) {
if (b.length() != a.length() + 1) return false;
int i = 0, j = 0;
for (i = 0, j = 0; i < a.length(); i++, j++) {
if (b.at(j) == a.at(i)) {
continue;
} else {
j++;
}
if (j - i >= 2) {
return false;
}
}
if (j - i >= 2) {
return false;
} else {
return true;
}
}
void dfs(int index, vector<bool>& visited, vector<vector<char>>& link,
vector<string>& words, int depth, int& large) {
// visited[index] = true;
int d = depth + 1;
if (d > large) {
large = d;
}
visited[index] = true;
for (auto i = 0; i < visited.size(); i++) {
if (link[index][i] == 0) {
visited[i] = true;
continue;
}
if (link[index][i] == 1) {
dfs(i, visited, link, words, d, large);
continue;
}
if (link[index][i] == -1) {
if (isLinked(words[index], words[i])) {
link[index][i] = 1;
dfs(i, visited, link, words, d, large);
continue;
} else {
link[index][i] = 0;
visited[i] = true;
continue;
}
}
}
}
int longestStrChain(vector<string>& words) {
const int N = words.size();
auto link = vector<vector<char>>(N, vector<char>(N, -1));
int large = 0;
for (int i = 0; i < N; i++) {
auto visited = vector<bool>(N, false);
dfs(i, visited, link, words, 0, large);
}
return large;
}
};
int main(int argc, char const* argv[]) {
auto sol = new Solution();
vector<string> v = {"xbc", "pcxbcf", "xb", "cxbc", "pcxbc"};
auto ret = sol->longestStrChain(v);
cout << ret << endl;
return 0;
}