-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1019.cpp
75 lines (69 loc) · 1.12 KB
/
1019.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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define IL inline
#define re register
#define LL long long
using namespace std;
string word[25];
char start;
int n;
int book[25];
int maxans;
int match(string a,string b)
{
// cout<<"Æ¥Åä"<<a<<" "<<b<<endl;
for(int i=1;i<min(a.size(),b.size());i++)
{
// cout<<"i:"<<i<<endl;
bool flag=1;
for(int j=0;j<i;j++)
{
// cout<<"j:"<<j<<endl;
if(b[j]!=a[a.size()-i+j]){
flag=0;
break;
}
}
if(flag) return i;
}
return 0;
}
void dfs(int len,int last)
{
maxans=max(len,maxans);
for(int i=0;i<n;i++)
if(book[i]<2)
{
if(match(word[last],word[i]))
{
book[i]++;
// cout<<"¼ÓÈë"<<word[i]<<endl;
dfs(len+word[i].size()-match(word[last],word[i]),i);
// cout<<"stop"<<word[i]<<"\n";
book[i]--;
}
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++) cin>>word[i];
cin>>start;
for(int i=0;i<n;i++)
{
if(word[i][0]!=start) continue;
book[i]++;
// cout<<"Ê×Ñ¡"<<i<<endl;
dfs(word[i].size(),i);
book[i]--;
}
cout<<maxans;
return 0;
}