Mix and Build(简单DP)
来源:互联网 发布:js如何封装 编辑:程序博客网 时间:2024/06/16 09:27
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 0x3f3f3f3fusing 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
- poj2004——Mix and Build
- POJ 2004 Mix and Build 笔记
- mix QML and C++
- Mix C and C++
- mix-ins的简单介绍
- MIX
- How to mix C and C++
- HOW TO MIX C AND C++
- AutoSize and DockStyle.Fill don't mix
- How to mix C and C++
- hdu 1069 Monkey and Banana(简单dp)
- ZOJ3735 Josephina and RPG(简单DP)
- Sweet and Sour Rock+spoj+简单dp
- 761C Dasha and Password[简单dp]
- [简单DP]Monkey and Banana HDU
- Checked build and Ratial build
- 设置Spring定时器
- jquery有用却不常用的方法分享
- 理解快速排序法。
- ActivityManager: Warning: Activity not started, its current task has been brought to the front问题
- android学习----overridePendingTransition
- Mix and Build(简单DP)
- Gitlab常用操作
- HDU 5253 连接的管道
- 大型网站动态应用系统架构
- yii2.0发送邮件发不出去问题记录
- 详细学习之java中的日期转换
- WaitForSingleObject函数用法及互斥对象的理解 .
- Spring inner bean examples
- forward与include转发问题(RequestDispatcher.include()(乱码问题分析及解决)