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. 不等高cell的tableView界面搭建

    一.搭建界面 1.界面分析 分析界面的层次结构,分析界面应该用什么控件来搭建 2.界面层次结构 分析之后,我们可以把这个界面分为四个模块(topView middleView commentView ...

  2. leetcode98 Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  3. How about xlogs are missing and xlogs are deleted

    [postgres@minion1 bin]$ pwd /usr/local/pgtest/bin [postgres@minion1 bin]$ ./pg_ctl -D ../data/ start ...

  4. Leetcode: Mini Parser

    Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...

  5. 也不知怎么了LVS.SH找不到,网上搜了一篇环境搭配CENTOS下面的高可用 参考

    系统环境: ************************************************************ 两台服务器都装了 CentOS-5.2-x86_64 系统 Vir ...

  6. swt byte[] 与 Image的转换

    1. 从byte[]得到Image private static Image createImage(byte[] imageBytes) { Image image = null; try { By ...

  7. docker gitlab

    Alternatively, you can manually launch the gitlab container and the supporting postgresql and redis ...

  8. Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别

    Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别 时间 2012-12-19 20:30:12  CSDN博客原文  http://blog.cs ...

  9. 4. 星际争霸之php设计模式--工厂方法模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON DirectFile

    zw版[转发·台湾nvp系列Delphi例程]HALCON DirectFile unit Unit1;interfaceuses Windows, Messages, SysUtils, Varia ...