Mix and Build(简单DP)
2015-08-21 14:32
295 查看
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,找最长字符串链,使后一个比前一个的长度大一,并且只有一个字符不同
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; }
相关文章推荐
- UITextField详解
- 解决query不到本地视频方法
- 设置UIView的背景色和背景图片
- QT 总结(四) 1.中文编码 乱码 2.QFileDialog 打开文件框 3.调用其它UI文件 4.QMessageBox弹窗
- How To Build Compelling Stories From Your Data Sets
- HDU 1387 Team Queue(优先队列)
- 这里想经过一个小程序研究标准库为 vector 对象提供的内存分配策,因为vector容器比list和deque容器用的很多,而且它的存储方式是连续的
- 自定义UIAlertView及简单的回调函数使用说明
- 关于uboot的CONFIG_SPL_BUILD宏的一个小知识点
- [HDU 1530] Maximum Clique 最大团
- Espresso实战:快速的Android UI自动化测试
- 辛星简译MySQL中的last_query_cost
- @GeneratedValue 四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
- C#中的属性(get、set、value)
- C#中的属性(get、set、value)
- The GNU configure and build system-configure的详细介绍
- UITableView 多层详解
- UITextfield后面只能输入小数点后2位
- IOS UIPageControl(分页控制器) 用法总结
- Android常用UI组件属性和.9图制作