1165. 单词环 (01分数规划,建图,经验值优化,正环)

该文介绍如何通过图论和01分数规划解决字符串环问题,寻找使字符串首尾相连形成环的最长平均长度。
摘要由CSDN通过智能技术生成

1165. 单词环 - AcWing题库

我们有 n 个字符串,每个字符串都是由 a∼z 的小写英文字母组成的。

如果字符串 A 的结尾两个字符刚好与字符串 B 的开头两个字符相匹配,那么我们称 A 与 B 能够相连(注意:A 能与 B 相连不代表 B 能与 A 相连)。

我们希望从给定的字符串中找出一些,使得它们首尾相连形成一个环串(一个串首尾相连也算),我们想要使这个环串的平均长度最大。

如下例:

ababc
bckjaca
caahoynaab

第一个串能与第二个串相连,第二个串能与第三个串相连,第三个串能与第一个串相连,我们按照此顺序相连,便形成了一个环串,长度为 5+7+10=22(重复部分算两次),总共使用了 3 个串,所以平均长度是 22/3≈7.33。

输入格式

本题有多组数据。

每组数据的第一行,一个整数 n,表示字符串数量;

接下来 n 行,每行一个长度小于等于 1000 的字符串。

读入以 n=0 结束。

输出格式

若不存在环串,输出”No solution”,否则输出最长的环串的平均长度。

只要答案与标准答案的差不超过 0.01,就视为答案正确。

数据范围

1≤n≤105

输入样例:
3
intercommunicational
alkylbenzenesulfonate
tetraiodophenolphthalein
0
输出样例:
21.66

解析: 

本题三个重点:

1.建图:将每个字符串看成一条边,边权为字符串的长度,边的两个端点为字符串首尾的两个字符

2.01分数规划:上一篇题解有详细讲解,​​​​​​​361. 观光奶牛(小数二分,spfa判断正环,01分数规划)-CSDN博客​​​​​​​

3.优化:加一个经验值判断,循环次数超过一个经验值之后,就有很大概率存在正环。

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<sstream>
#include<deque>
#include<unordered_map>
using namespace std;
const int N = 700 , M = 1e5 + 5;
int n;
int h[N], e[M], w[M], ne[M], idx;
double dist[N];
int vis[N], cnt[N];

void add(int a, int b, int c) {
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int check(double m) {
	queue<int>q;
	memset(vis, 0, sizeof vis);
	memset(cnt, 0, sizeof cnt);
	for (int i = 0; i < N; i++) {
		q.push(i);
		vis[i] = 1;
	}
	int count = 0;
	while (!q.empty()) {
		int t = q.front();
		q.pop();
		vis[t] = 0;
		for (int i = h[t]; i != -1; i = ne[i]) {
			int j = e[i];
			if (dist[j] < dist[t] + w[i] - m) {
				dist[j] = dist[t] + w[i] - m;
				cnt[j] = cnt[t] + 1;
				if (++count > 1e4)return 1;
				if (cnt[j] >= N)return 1;
				if (!vis[j]) {
					vis[j] = 1;
					q.push(j);
				}
			}
		}
	}
	return 0;
}


int main() {
	while (cin >> n, n) {
		char str[1005];
		memset(h, -1, sizeof h);
		idx = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%s", str);
			int len = strlen(str);
			if (len >= 2) {
				int left = (str[0] - 'a') * 26 + str[1] - 'a';
				int right = (str[len - 2] - 'a') * 26 + str[len - 1] - 'a';
				add(left, right, len);
			}
		}
		if (!check(0))cout << "No solution" << endl;
		else {
			double l = 0, r = 1e3, mid;
			while (r-l>1e-4) {
				mid = (l + r) / 2;
				if (check(mid))l = mid;
				else r = mid;
			}
			cout << r << endl;
		}
	}
	return 0;
}

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值