Mix and Build(简单DP)
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)的更多相关文章
- POJ2004 Mix and build Trie树? dp?
学习Trie树中,所以上网搜一下Trie树的题,找到这个,人家写着是简单dp,那我就想着能学习到什么Trie树上的dp,但最后发现根本好像跟Trie树没有什么联系嘛... 题意就是给你很多个字符串(长 ...
- Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出
题意: 就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串, 其中之前的m个串每出现一次就算一次那个字符串的权值, 求能找到的最大权值的字符串,如果存在多个解,输出最短的字典 ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- 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 ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
随机推荐
- Lintcode: Maximum Subarray II
Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...
- exec 临时表,报错
因为零时表只存在于一个exec 会话中,所以可以用 多个 select 返回到 dataset 中处理多个table,按照select 的顺序,读取 tables[0],table[1] , 多用于统 ...
- demo15 AlertDialog
Dialog dialog = new AlertDialog.Builder(this).setTitle("对话框").setMessage("this is msg ...
- c语言小程序
这是一个用c语言写的小程序,功能是随机输出30道100以内的四则运算,先生成两个随机数,再通过随机数确定四则运算符号,最后输出题目. #include<iostream> using na ...
- MVC4 下DropDownList使用方法
与MVC3相比,差别很大: 表现形式一: public ActionResult Main() { List<SelectListItem> items = new List<Sel ...
- 06---Net基础加强
字符串特性-池-不可变性 class Program { static void Main(string[] args) { #region 判断两个对象是否是同一个对象 //Person p1 = ...
- 夺命雷公狗ThinkPHP项目之----企业网站16之文章列表页的完善(关联查询)
我们栏目的所属栏目不能总是以数字来显示吧??这样的话,估计老板会让您直接卷铺盖滚蛋噢,嘻嘻... 所以我们需要对她进行关联查询,控制器代码如下所示: public function lists(){ ...
- 夺命雷公狗---Thinkphp----15之遍历出来的栏目页的完成
我们首页的写法和我们的文章页的代码很相似,我们要在点击我们的栏目页的时候遍历出对应的代码: 那么我们就直接来创建一个ListsController.class.php的文件,代码如下所示: 老规矩遍历 ...
- LDA-math-神奇的Gamma函数
http://cos.name/2013/01/lda-math-gamma-function/ 1. 神奇的Gamma函数1.1 Gamma 函数诞生记学高等数学的时候,我们都学习过如下一个长相有点 ...
- PAT乙级 1016. 部分A+B (15) C语言实现
1016. 部分A+B (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 正整数A的“DA(为1位整数)部 ...