#include <bits/stdc++.h>
using namespace std;
inline int read() {
int s = 0, f = 1;
char a = getchar();
while (a > '9' || a < '0') {
if (a == '-')
f = -1;
a = getchar();
}
while (a >= '0' && a <= '9') {
s = (s << 1) + (s << 3) + (a ^ 48);
a = getchar();
}
return s * f;
}
#define il inline
const int maxm = 1e5 + 10, maxn = 1000;
const double eps = 1e-6;
struct uzi {
int to, next, w;
} edge[maxm];
int head[maxn], tot = 0;
il void add(int x, int y, int w) {
edge[++tot] = {y, head[x], w};
head[x] = tot;
}
il int cal(char a, char b) {
return (a - 'a') * 31 + (b - 'a') + 1;
}
bool vis[maxn];
double dis[maxn];
bool spfa(int v, double mid) {
vis[v] = true;
for (int i = head[v]; i; i = edge[i].next) {
int y = edge[i].to;
if (dis[v] + edge[i].w - mid > dis[y]) {
dis[y] = dis[v] + edge[i].w - mid;
if (vis[y] || spfa(y, mid)) {
vis[y] = false;
return true;
}
}
}
vis[v] = false;
return false;
}
bool check(double mid) {
memset(dis, 0, sizeof(dis));
for (int i = 1; i <= 31 * 31; i++)
if (spfa(i, mid))
return true;
return false;
}
char st[1005];
int n;
int main() {
while (cin >> n && n) {
memset(head, 0, sizeof(head));
tot = 0;
for (int i = 1; i <= n; i++) {
scanf("%s", st + 1);
int len = strlen(st + 1);
add(cal(st[1], st[2]), cal(st[len - 1], st[len]), len);
}
double l = 0, r = 1005;
while (r - l > eps) {
double mid = (l + r) / 2;
if (check(mid))
l = mid;
else
r = mid;
}
if (l == 0)
puts("No solution");
else
printf("%.3lf\n", l);
}
return 0;
}
一本通1504:【例 1】Word Rings
最新推荐文章于 2024-11-11 20:45:30 发布