poj2949 word rings

该程序设计任务是根据给出的单词列表构建一个单词环,要求平均单词长度最大化。输入包含多个测试用例,每个用例包含单词数量及单词本身,单词长度不超过1000。如果无法构成单词环,则输出'No solution.'。解决方案建议使用二分搜索配合SPFA算法判断环的存在,并处理可能的精度误差。
摘要由CSDN通过智能技术生成

http://www.elijahqi.win/archives/1168
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 le

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值