題目:已知一些字典序排列的單詞,問能從中找到最大的一個有序單詞集合,

使得集合中的單詞每一個是有上一個單詞經過一次變換得來的(增、刪、改)。

分析: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的更多相关文章

  1. UVA - 10029 Edit Step Ladders (二分+hash)

    Description Problem C: Edit Step Ladders An edit step is a transformation from one word x to another ...

  2. UVA 10029 Edit Step Ladders ——(DAG求最长路)

    题意:升序的给出一本若干个单词,每个单词都可删除一个字母,添加一个字母或者改变一个字母,如果任意一个操作以后能变成另外一个字典中的单词,那么就连一条有向边,求最长的长度. 分析:DAG的最长路和最短路 ...

  3. Edit Step Ladders - UVA 10029

    题意 题目链接(Virtual Judge):Edit Step Ladders - UVA 10029 题意: 如果单词 \(x\) 能通过添加.删除或修改一个字母变换为单词 \(y\),则称单词 ...

  4. uva 10026 Problem C: Edit Step Ladders

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. POJ2564:Edit Step Ladders

    浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:http://poj.org/problem?id=2564 记\(f[i ...

  6. UVa 10029 hash + dp

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  8. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  9. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

随机推荐

  1. scrapy框架系列 (3) Item Pipline

    item pipeline 当Item在Spider中被收集之后,它将会被传递到Item Pipeline,这些Item Pipeline组件按定义的顺序处理Item. 每个Item Pipeline ...

  2. 第一个手写Win32窗口程序

    第一个手写Win32窗口程序 一 Windows编程基础 1 Win32应用程序的基本类型 1.1 控制台程序 不需要完善的Windows窗口,可以使用DOS窗口 的方式显示. 1.2 Win32窗口 ...

  3. php命名空间的使用,同一个命名空间可以在多个文件中定义

    php namespace的使用,直接打印出已经定义的命名空间 直接上代码,a.php , b.php, c.php , main.php a.php <?php namespace A{ cl ...

  4. Structured Streaming编程向导

    简介 Structured Streaming is a scalable and fault-tolerant stream processing engine built on the Spark ...

  5. JAVA 中不错的开源FTP组件:commons-net

    第一步:引入jar到pom.xml. <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dep ...

  6. Python中Json解析的坑

    JSON虽好,一点点不对,能把人折腾死: 1.变量必须要用双引号 2.如果是字符串,必须要用引号包起来 Error:Expecting : delimiter: line 1 column 6 (ch ...

  7. Creating OpenGL 4.3 window fails with GLFW3

      I set up a minimal application to open a blank window with GLFW3: #include <iostream> #inclu ...

  8. ASP入门(十七)-ASP #include

    通过使用 #include 指令,您可以在服务器执行 ASP 文件之前,把另一个 ASP 文件的内容插入到这个 ASP 文件中. 如何使用 #include 指令 这里有一个名为 mypage.asp ...

  9. Python编程工具pycharm的使用

    简介 俗话说工欲善其事必先利其器,所以对于程序员来说,使用python编程必须有一个强大的Python编程工具,这款工具就是pycharm. PyCharm是一种Python IDE,带有一整套可以帮 ...

  10. [Canvas]游戏增加怪物爆炸效果,改善箭头形状

    请点此下载代码并用浏览器打开试玩. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-eq ...