UVa 10029 - Edit Step Ladders
題目:已知一些字典序排列的單詞,問能從中找到最大的一個有序單詞集合,
使得集合中的單詞每一個是有上一個單詞經過一次變換得來的(增、刪、改)。
分析:dp,LIS。最大遞增子序列,不過數據較大须要優化。
因為,不是每一個單詞都是还有一個單詞的前驅,所以非常多查找是不必要的。
所以,利用每個單詞進行變換求出全部前驅,然後查找前驅是否在前面出現過;
假设出現過。則能够更新LIS。查找前驅的過程使用二分優化就可以節約時間。
說明:本來先用hash表+記憶化搜索,效率較低╮(╯▽╰)╭。
#include <cstring>
#include <cstdio> #define max(a, b) ((a)>(b)?(a):(b)) char list[25005][17], now[17], New[17];
int dp[25005]; int binary_search(int r, char words[])
{
int l = 0, mid = 0, cmp;
while (l <= r) {
mid = (l+r)>>1;
cmp = strcmp(list[mid], words);
if (!cmp) return mid;
if (cmp > 0) r = mid-1;
else l = mid+1;
}
return -1;
} int main()
{
int size = 0;
while (~scanf("%s",list[size])) ++ size; int maxv = 0;
for (int p = 0; p < size; ++ p) {
dp[p] = 1;
strcpy(now, list[p]);
int length = strlen(now), point = -1;
//删除元素
for (int i = 0; i < length; ++ i) {
if (now[i] < now[i+1]) continue;
int count = 0;
for (int j = 0; j < length; ++ j)
if (i != j) New[count ++] = now[j];
New[count ++] = 0;
point = binary_search(p-1, New);
if (point >= 0 && dp[p] <= dp[point])
dp[p] = dp[point]+1; }
//加入元素
for (int i = 0; i <= length; ++ i)
for (char k = 'a'; k <= 'z'; ++ k) {
if (k > now[i]) continue;
int count = 0;
for (int j = 0; j <= length; ++ j) {
if (i == j) New[count ++] = k;
New[count ++] = now[j];
}
New[count ++] = 0;
point = binary_search(p-1, New);
if (point >= 0 && dp[p] <= dp[point])
dp[p] = dp[point]+1;
}
//改变元素
for (int i = 0; i < length; ++ i)
for (char k = 'a'; k < now[i]; ++ k) {
strcpy(New, now);
New[i] = k;
point = binary_search(p-1, New);
if (point >= 0 && dp[p] <= dp[point])
dp[p] = dp[point]+1;
}
maxv = max(maxv, dp[p]);
} printf("%d\n",maxv);
return 0;
}
UVa 10029 - Edit Step Ladders的更多相关文章
- UVA - 10029 Edit Step Ladders (二分+hash)
Description Problem C: Edit Step Ladders An edit step is a transformation from one word x to another ...
- UVA 10029 Edit Step Ladders ——(DAG求最长路)
题意:升序的给出一本若干个单词,每个单词都可删除一个字母,添加一个字母或者改变一个字母,如果任意一个操作以后能变成另外一个字典中的单词,那么就连一条有向边,求最长的长度. 分析:DAG的最长路和最短路 ...
- Edit Step Ladders - UVA 10029
题意 题目链接(Virtual Judge):Edit Step Ladders - UVA 10029 题意: 如果单词 \(x\) 能通过添加.删除或修改一个字母变换为单词 \(y\),则称单词 ...
- uva 10026 Problem C: Edit Step Ladders
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- POJ2564:Edit Step Ladders
浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:http://poj.org/problem?id=2564 记\(f[i ...
- UVa 10029 hash + dp
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
随机推荐
- libxml2 使用教程【转】
https://blog.csdn.net/zhoudaxia/article/details/8565731# 本文整理自官方使用教程http://xmlsoft.org/tutorial/inde ...
- LeetCode295-Find Median from Data Stream && 480. 滑动窗口中位数
中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2.5 设计一个支持以下两种操 ...
- Unique Binary Search Trees II leetcode java
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- Android之ASD组件(一)
Google在android5.0之后推出新设计标准Material Design,为了能在低版本上使用Material Design,google发布了Android Support Design支 ...
- Android -- Property Animation
3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...
- Android -- Camera2(Android5.0)
Camera2 Camera2是Android5.0中的其中一个新的特性,新的API.与原来的camera API相比,不同之处在于: 原生支持RAW照片输出 突发拍摄模式 制约拍照速度的不再是软件而 ...
- 【Java】Java-fastjson-基本使用方法
Java-fastjson-基本使用方法 fastjson maven_百度搜索 Maven Repository: com.alibaba » fastjson » 1.2.44 fastjson ...
- 转:从头开始编写基于隐含马尔可夫模型HMM的中文分词器
http://blog.csdn.net/guixunlong/article/details/8925990 从头开始编写基于隐含马尔可夫模型HMM的中文分词器之一 - 资源篇 首先感谢52nlp的 ...
- mysql的水平拆分和垂直拆分 (转)
http://www.cnblogs.com/sns007/p/5790838.html 1,水平分割: 例:QQ的登录表.假设QQ的用户有100亿,如果只有一张表,每个用户登录的时候数据库都要从这1 ...
- 一道Javascript面试题引发的血案
文章首发于szhshp的第三边境研究所,转载请注明 先来看几道面试题,公司的开发们都尝试做了一下,然而基本没有人能够全部答对. 覆盖的考点很多,也有一些难度,题目挺有意思建议手动执行一边玩玩. Que ...