poj 2004

 

Mix and Build
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3282 Accepted: 974
Case Time Limit: 2000MS Special Judge

Description

In this problem, you are given a list of words (sequence of lower case letters). From this list, find the longest chain of words w1, ..., wn such that wi is a mixed extension of w i-1. A word A is a mixed extension of another word B if A can be formed by adding one letter to B and permuting the result. For example, "ab", "bar", "crab", "cobra", and "carbon" form a chain of length 5.

Input

The input contains at least two, but no more than 10000 lines. Each line contains a word. The length of each word is at least 1 and no more than 20. All words in the input are distinct.

Output

Write the longest chain that can be constructed from the given words. Output each word in the chain on a separate line, starting from the first one. If there are multiple longest chains, any longest chain is acceptable.

Sample Input

ab
arc
arco
bar
bran
carbon
carbons
cobra
crab
crayon
narc

Sample Output

ab
bar
crab
cobra
carbon
carbons

Source

分析:这题有多种解法,暴力貌似都行,囧~~~先把每个单词自身按字母顺序重新拼写,即bca-->abc,然后按单词长度排序,相同长度

         按字典顺序排,之后很轻易想到DP,枚举每个单词i为结尾有,查找可以和它连接的单词j,

         f[i]=max(f[i],f[j]+1){条件:i可以由j得到}

          此处的查找可以多方法,hash还是蛮快的,二分也还好。。。话说一开始好像重构但是错了,郁闷了一天~~~

代码1(二分查找):

 

代码2(字符串hash,现学现用,哈哈):

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值