AC100:
```cpp
#include<bits/stdc++.h>
#define _for(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define _all(i, a, b) for(int i = (a); i <= (int)(b); ++i)
#define elif(except) else if(except)
#define ifn(except) if(!(except))
#define lowbit(x) ((x) & (-(x)))
using namespace std;
typedef long long ll, LLONG;
const int N = 128;
int n, ans = 0;
vector<string> G[N];
map<string, int> Vis;
void dfs(string& s, int cnt = 1) {
ans = max(ans, (int)(s.size()));
if(cnt == n) return ;
_for(i, 1, s.size()) {
int num = s.size() - i;
for(auto& p : G[s[i]]) {
if((int)(p.size()) <= num) continue;
if(s.substr(i) == p.substr(0, num) && Vis[p] < 2) {
++Vis[p];
string t = s + p.substr(num);
dfs(t, cnt + 1);
--Vis[p];
}
}
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for(string s; n--; G[s[0]].push_back(s)) cin >> s;
char c; cin >> c;
for(auto& s : G[c]) {
Vis[s] = 1;
dfs(s);
Vis[s] = 0;
}
cout << ans;
return 0;
}
```
by xushikai @ 2023-09-28 08:52:05