Mix and Build

Time Limit: 5000MS Memory Limit: 65536K

Total Submissions: 3936 Accepted: 1203

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 wi-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

Rocky Mountain 2004

简单的Dp,找最长字符串链,使后一个比前一个的长度大一,并且只有一个字符不同

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef unsigned long long LL; const int MAX = 1e5+10; struct node
{
char str[25];
int Hash[26];
int len;
int Dp;
int pre;
void init()//初始化
{
memset(Hash,0,sizeof(Hash));
len=0;
Dp=1;
pre=-1;
}
void HASH()//哈希字符
{
len=strlen(str);
for(int i=0;i<len;i++)
{
Hash[str[i]-'a']++;
}
}
void Output()
{
printf("%s\n",str);
}
}Ch[11000]; bool cmp(node a,node b)//按照长度进行排序
{
return a.len<b.len;
} void DFS(int s)//输出
{
if(s==-1)
{
return ;
}
DFS(Ch[s].pre);
Ch[s].Output();
} int main()
{
// freopen("input.txt","r",stdin);
for(int i=0;i<10001;i++)
{
Ch[i].init();
}
int top=0;
while(~scanf("%s",Ch[top].str))
{
Ch[top].HASH();
top++;
}
sort(Ch,Ch+top,cmp);
for(int i=0;i<top;i++)
{
for(int j=i-1;j>=0;j--)
{
if(Ch[j].len==Ch[i].len)
{
continue;
}
if(Ch[j].len==Ch[i].len-1)
{
int ans=0;
for(int k=0;k<26;k++)
{
if(Ch[i].Hash[k]!=Ch[j].Hash[k])
{
ans++;
}
if(ans>2)
{
break;
}
}
if(ans<2)//如果不同的字符大于两个则不符合
{
if(Ch[i].Dp<Ch[j].Dp+1)
{
Ch[i].Dp=Ch[j].Dp+1;
Ch[i].pre=j;
}
}
}
else
{
break;
}
}
}
int Max=0,ans;
for(int i=0;i<top;i++)
{
if(Max<Ch[i].Dp)
{
Max=Ch[i].Dp;
ans=i;
}
}
DFS(ans);
return 0;
}

Mix and Build(简单DP)的更多相关文章

  1. POJ2004 Mix and build Trie树? dp?

    学习Trie树中,所以上网搜一下Trie树的题,找到这个,人家写着是简单dp,那我就想着能学习到什么Trie树上的dp,但最后发现根本好像跟Trie树没有什么联系嘛... 题意就是给你很多个字符串(长 ...

  2. Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出

    题意: 就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串, 其中之前的m个串每出现一次就算一次那个字符串的权值, 求能找到的最大权值的字符串,如果存在多个解,输出最短的字典 ...

  3. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  5. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  6. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  7. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  8. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  9. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

随机推荐

  1. 文字排版一律用plist

    NSMutableArray *newhealthArr = [NSMutableArray new]; NSArray *healthTitleArr = [NSArray arrayWithCon ...

  2. Adobe Flash CC 安装报错的解决办法

    安装FlashCC的时候莫名的报错 ---------------------------Flash.exe - 应用程序错误---------------------------应用程序无法正常启动 ...

  3. MyEclipse下如何安装和使用ibatis插件(网上的资料对于myeclipse8.5根本就是没有用的,所以我还是自己选择了装了一个eclipse,然后将插件装在了eclipse中)

    (1)myeclipse→help→Myeclipse configuration center:点击sofeware选项卡,在Browes Software 下有一个输入框,点击add site按钮 ...

  4. HDU 4898 The Revenge of the Princess’ Knight(后缀数组+二分+暴力)(2014 Multi-University Training Contest 4)

    Problem Description There is an old country and the king fell in love with a devil. The devil always ...

  5. Android中实现app版本更新

    1,获取本地程序apk版本,并开启服务(下面这段代码一般在主Activity中的onCreate()方法中执行的,并开启后台服务下载新版本的apk) //获取apk包文件的管理者对象 PackageM ...

  6. php操作oracle的方法类集全

    在网上开始找php中操作oracle的方法类~ 果然找到一个用php+oracle制作email表以及插入查询的教程,赶忙点开来看,从头到尾仔细的看了一遍,还没开始操作,便觉得收获很大了.地址在此:h ...

  7. JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总

    一下纯属个人总结摘抄,总结一起方便查看,解决疑问,有遗漏或错误,还请指出.       1,JSTL标签总结: a).JSTL标签有什么用?          JSTL是由JCP(Java Commu ...

  8. lower power设计中的DVFS设计

    Pswitch = Ceff * Vvdd^2*Fclk, Pshort-circuit = Isc * Vdd * Fclk, Pleakage = f(Vdd, Vth, W/L) 尽管对电压的s ...

  9. 「Ruby && Sqlite3」How to install sqlite3 for ruby? (solve: sqlite-ruby no such file...)

    error message:           no such file .... 安装 gem install sqlite3-ruby -- --with-sqlite3-dir=/usr/lo ...

  10. 使用sublime text3的一些事

    因为在第一次接触网页设计的时候,使用的是Dreamweaver,它的设计是一款所见即所得的网页编辑器,而且当你写好元素之后的“:”时,就会有代码提示功能,对一些初学者来说,是一个不错的选择.但是慢慢地 ...