POJ 2949 Word Rings(差分约束)

本文介绍了一种寻找最大平均值字环的算法实现。通过构建图结构并使用DFS进行搜索,结合二分查找确定最优平均值。文章还提到了使用哈希表提升效率的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:找平均值最大的环,将字符串前两个和最后两个当做两个结点,有一条边权值就是这个串的长度。

           那么avg = (w1+w2+....wk)/k,把k乘过去有avg*k = (w1+w2+...wk),然后有(w1-avg)+(w2-avg)+...(wk-avg)>=0,然后其实就是要求(w1-ans)+(w2-ans)+...(wk-ans)>0,二分ans然后求正环。

           用DFS找环会比BFS快上不少...另外这个题用hash比用map会快很多...


#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 100000+5;
vector<pair<int,int> >e[maxn];
map<string,int>mp;
double d[maxn];
int inq[maxn],cnt,n;
bool dfs(int u,double avg)
{
    inq[u]=1;
	for(int i = 0;i<e[u].size();i++)
	{
		int v = e[u][i].first;
		if(d[v]<d[u]+e[u][i].second-avg)
		{
			d[v]=d[u]+e[u][i].second-avg;
			if(inq[v]||dfs(v,avg))
				return true;
		}
	}
	inq[u]=0;
	return false;
}
bool check(double avg)
{
	memset(d,0,sizeof(d));
	memset(inq,0,sizeof(inq));
	for(int i = 1;i<=n;i++)
		if(dfs(i,avg))return true;
	return false;
}
int main()
{
    while(scanf("%d",&n)!=EOF && n)
	{
		for(int i=0;i<=n;i++)
			e[i].clear();
		//mp.clear();
		string s;cnt=1;
		for(int i = 0;i<n;i++)
		{
			string tmp=""; 
            cin >> s;
			tmp+=s[0];
			tmp+=s[1];
            if(!mp.count(tmp))
				mp[tmp]=cnt++;
			string tmp1="";
			tmp1+=s[s.size()-2];
			tmp1+=s[s.size()-1];
			if(!mp.count(tmp1))
				mp[tmp1]=cnt++;
            e[mp[tmp]].push_back(make_pair(mp[tmp1],s.size()));
		}
		n = mp.size();
		double ans = -1;
		double l = 0,r=1000;
		for(int i = 0;i<100;i++)
		{
			double mid = (l+r)/2;
			if(check(mid))
				l=mid,ans=mid;
			else
				r=mid;
		}
		if(ans==-1)
			printf("No solution.\n");
		else
			printf("%.2f\n",ans);
	}
}

Description

A word ring is a sequence of words where the last two letters of each word are the same as the first two letters of the next word (and the last two letters of the last word are the same as the first two letters of the first word). For example, the following sequence is a word ring: 

intercommunicational 
alkylbenzenesulfonate 
tetraiodophenolphthalein 

Your task is to write a program that, given a list of words, finds a word ring. You have to make the word ring as impressive as possible: the average length of the words in the ring has to be as large as possible. In the above example, the average length is (20 + 21 + 24)/3 ≈ 21.6666 , which makes it somewhat impressive. Note that each word can be used at most once in the ring, and the ring can consist of a single word. 

Input

The input contains several blocks of test cases. Each case begins with a line containing a single integer 1 ≤ n ≤ 100000 , the number of possible words that can be used. The next n lines contain these words. The words contain only the characters `a'-`z' and the length of each word is at most 1000. 

The input is terminated by a block with n = 0 . 

Output

For each test case in the input, you have to output a single number on a separate line: the maximum average length of a ring composed from (a subset of) the words given in the input. The average length should be presented as a real number with two digits of precision. If it is not possible to compose a ring from these words, then output `No solution.' (without quotes). To avoid rounding problems, we accept solutions with a maximum of ±0.01 error.

Sample Input

3
intercommunicational
alkylbenzenesulfonate
tetraiodophenolphthalein
0

Sample Output

21.66

Hint

Huge input file, 'scanf' recommended to avoid TLE. 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值