-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1019.cpp
71 lines (70 loc) · 1.39 KB
/
P1019.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
#include <bits/stdc++.h>
using namespace std;
int n;
string word[21], start;
int max_length;
int times[21];
int mat(string x, string y)
{
for (int s1 = x.length() - 1; s1 >= 0; s1--)
{
bool flag = true;
for (int s2 = s1; s2 < x.length(); s2++)
{
if (s2 - s1 >= y.length() || x[s2] != y[s2 - s1])
{
flag = false;
break;
}
}
if (x.length() != 1 && s1 == 0) // 不让y包含x
{
continue;
}
if (x.length() - s1 == y.length())
{
continue;
}
if (flag)
{
return s1;
}
}
return -1;
}
void dfs(string head)
{
bool flag = false;
for (int i = 1; i <= n; i++)
{
if (times[i] >= 2)
{
continue;
}
int pol = mat(head, word[i]);
if (pol != -1)
{
flag = true;
string back_head = head;
head += word[i].substr(head.length() - pol);
times[i]++;
max_length = max(max_length, (int)head.length());
dfs(head);
// 恢复状态
times[i]--;
head = back_head;
}
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> word[i];
}
cin >> start;
dfs(start);
cout << max_length;
return 0;
}