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. 使用Universal USB Installer安装Ubuntu

    1.下载Universal USB Installer 下载地址: 2.下载ubuntu 14 desktop.iso 运行Universal USB Installer,找到电脑上 ubuntu 1 ...

  2. Object-C 入门

    该文章转载自:http://sheng.iteye.com/blog/775588一:Objective-C入门 .Cocoa的组成 苹果公司将Cocoa.Carbon.QuickTime和OpenG ...

  3. ASP.NET的一般处理程序对图片文件的基本操作

    以一个小项目为例: 验证码: public class VerifyCodeHelper { public VerifyCodeHelper() { this.ran = new Random(); ...

  4. 使用Eclipse构建Maven项目 (转)

    Maven这个个项目管理和构建自动化工具,越来越多的开发人员使用它来管理项目中的jar包.本文仅对Eclipse中如何安装.配置和使用Maven进行了介绍.完全step by step. 如果觉得本文 ...

  5. C++之路起航——标准模板库(deque)

    deque(双端队列):http://baike.baidu.com/link?url=JTvA2cuLubptctHZwFxswvlZvxNdFOxmifsYCGLj5IZF-Tj4rbWLv8Jn ...

  6. CSS_03_04_CSS伪元素选择器

    第01步:编写css代码:wei.css @charset "utf-8"; /* 伪元素选择器 :状态 效果顺序:L V H A */ a:link.lin_01{/*超链接,未 ...

  7. /Users/alamps/AndroidStudioProjects/Demo11ListView

    package com.example.demo11listview; import android.os.Bundle; import android.app.Activity; import an ...

  8. 夺命雷公狗ThinkPHP项目之----企业网站11之栏目的删除完成

    我们删除要在分类模型中添加一个_before_delete的钩子函数,而且在删除一个分类时候,如果这个分类有子分类就不允许删除 model层代码如下所示: <?php namespace Adm ...

  9. python3使用csv模块读写csv文件

    python3使用csv模块读写csv文件 读取csv文件: import csv #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() ...

  10. 【pyQuery分析实例】分析体育网冠军联盟比赛成绩

    目标地址:http://www.espncricinfo.com/champions-league-twenty20-2012/engine/match/574265.html liz@nb-liz: ...