-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlg1019.cpp
50 lines (44 loc) · 1.06 KB
/
lg1019.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
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int n;
vector<string> a;
int link[23][23];
int linkable(const string& a, const string& b) {
for (int i=0; i+1<b.size() && i+1<a.size(); ++i)
if (equal(b.begin(), b.begin()+i+1, a.end()-i-1))
return b.size() - i - 1;
return 0;
}
int dfs(int now, vector<int>& use) {
int ans = 0;
for (int i=0; i<n; ++i)
if (link[now][i] && use[i]<2) {
++use[i];
ans = max(ans, link[now][i] + dfs(i, use));
--use[i];
}
return ans;
}
int main() {
cin >> n;
a.resize(n);
for (int i=0; i<n; ++i) cin >> a[i];
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j)
link[i][j] = linkable(a[i], a[j]);
char head;
cin >> head;
vector<int> use(n);
int ans = 0;
for (int i=0; i<n; ++i)
if (a[i][0]==head) {
++use[i];
ans = max<int>(ans, a[i].size() + dfs(i, use));
--use[i];
}
cout << ans << endl;
return 0;
}