>Description
接龙游戏
给定一个字符为“头”和N个单词,求最长接龙长度
1.前后两个最少收尾相同部分重合在一起
2.可以自己连自己
3.每个单词可以用两次
>解题思路
考试的时候,以为只出现小写字母+最多重叠部分重叠在一起,导致考炸了T-T
说明以后写题的时候还是得细心点,题意不清晰的地方就要问
>代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100
using namespace std;
int n, ans, y[N], p[N][N], siz[N];
string s[N], hf[N][N], hb[N][N];
char c;
void dfs (int x, int cnt)
{
for (int i = 1; i <= n; i++)
{
if (y[i] == 2) continue;
if (p[x][i] == 0) continue;
y[i]++;
dfs (i, cnt + siz[i] - p[x][i]);
y[i]--;
}
ans = max (ans, cnt);
}
int main()
{
freopen ("english.in", "r", stdin);
freopen ("english.out", "w", stdout);
scanf ("%d", &n);
for (int i = 1; i <= n; i++)
{
cin >> s[i];
siz[i] = s[i].size();
s[i] = " " + s[i];
for (int j = 1; j <= siz[i]; j++)
hf[i][j] = hf[i][j - 1] + s[i][j];
for (int j = siz[i]; j >= 1; j--)
hb[i][j] = s[i][j] + hb[i][j + 1];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k < min (siz[i], siz[j]); k++)
if (hb[i][siz[i] - k + 1] == hf[j][k])
{p[i][j] = k; break;}
cin >> c;
for (int i = 1; i <= n; i++)
{
if (s[i][1] != c) continue;
y[i]++;
dfs (i, siz[i]);
y[i]--;
}
printf ("%d", ans);
return 0;
}