题意

题目链接(Virtual Judge):Edit Step Ladders - UVA 10029

题意:

如果单词 \(x\) 能通过添加、删除或修改一个字母变换为单词 \(y\),则称单词 \(x\) 到单词 \(y\) 的变换为一个 edit step。

Edit step ladder 指的是一个按字典序排列的单词序列 \(w_1,w_2,\ldots,w_n\),每个 \(w_{i+1}\) 都由 \(w_i\) 经一个 edit step 变换而来。

给出一个按字典序排列的单词序列,问其中符合 edit step ladder 要求的最长子序列的长度。

思路

虽然这题目又是字典序,又是子序列什么的,但其实跟字符串没多少关系,做法是建图跑最长路,将单词视为图节点,单词之间的变换视为有向边。想出建图这个思路后,剩下的就好办了。

首先是怎样建图的问题。如果采用枚举所有单词对的方法,时间复杂度 \(O(n^2)\),很可能会超时。我们注意到单词的长度非常短,所以不妨换一种思路:对于一个单词,枚举它能变换出的所有单词,这样便能实现 \(O(n)\) 建图。

具体做法如下:用哈希表保存单词及其对应下标。对于一个单词,枚举它经过一步 edit step 变换后的所有单词,看变换后在不在哈希表里。如果在,且下标大于当前单词(为了满足字典序要求),则构造一条有向边。

接着是怎样求解最长路的问题。显然此图是个有向无环图,所以我们可以用动态规划的方法 \(O(n)\) 求出最长路。

用 \(\mathrm{step}(u)\) 表示从 \(u\) 出发的最长路的长度(路径上的节点数),状态转移方程如下:

\[\mathrm{step}(u)=\left\{
\begin{array}{ll}
1 & \text{if}\; u \;\text{is a leaf} \\
1+\max\{\mathrm{step}(v) \mid (u,v)\in E\} & \text{otherwise} \\
\end{array}
\right.
\]

代码

注意:输出答案后还要再输出一个换行符,否则 UVA 会判你 WA(而不是 PE),非常的毒瘤。

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cassert> using namespace std; const int maxn = int(25000 + 5);
string word_arr[maxn];
unordered_map<string, int> word_dict;
vector<int> G[maxn];
int step_arr[maxn]; // 将 s 的第 i 个字符换成 ch
string change_letter(const string &s, int i, char ch)
{
string ret = s;
ret[i] = ch;
return ret;
} // 将 s 的第 i 个字符移除
string remove_letter(const string &s, int i)
{
string ret;
int len = int(s.length());
ret.reserve(len - 1);
for (int j = 0; j < len; j++)
if (j != i)
ret.push_back(s[j]);
return ret;
} // 在 s 的第 i 个字符前面插入字符 ch
string insert_letter(const string &s, int i, char ch)
{
string ret;
int len = int(s.length());
ret.reserve(len + 1);
if (i == len)
{
ret = s;
ret.push_back(ch);
return ret;
}
for (int j = 0; j < len; j++)
{
if (j == i)
ret.push_back(ch);
ret.push_back(s[j]);
}
return ret;
} // 添加有向边 u -> v
void add_edge(int u, int v)
{
G[u].push_back(v);
} // 从点 u 出发的最长路的长度
int longest_step(int u)
{
if (step_arr[u] > 0)
return step_arr[u];
int max_son_step = 0;
for (int v : G[u])
max_son_step = max(max_son_step, longest_step(v));
step_arr[u] = max_son_step + 1;
return step_arr[u];
} int main()
{
ios_base::sync_with_stdio(false);
string word;
int index = 1;
while (cin >> word)
{
word_arr[index] = word;
word_dict[word] = index;
index++;
}
int n = index - 1; for (int u = 1; u <= n; u++)
{
const string &cur = word_arr[u];
int len = int(cur.length());
// 修改字符
for (int i = 0; i < len; i++)
{
for (char ch = cur[i] + 1; ch <= 'z'; ch++)
{
string gen = change_letter(cur, i, ch);
if (word_dict.count(gen) > 0)
{
int v = word_dict[gen];
assert(u < v);
add_edge(u, v);
}
}
}
// 移除字符
for (int i = 0; i < len; i++)
{
string gen = remove_letter(cur, i);
if (word_dict.count(gen) > 0)
{
int v = word_dict[gen];
if (u < v)
add_edge(u, v);
}
}
// 插入字符
for (int i = 0; i <= len; i++)
{
for (char ch = 'a'; ch <= 'z'; ch++)
{
string gen = insert_letter(cur, i, ch);
if (word_dict.count(gen) > 0)
{
int v = word_dict[gen];
if (u < v)
add_edge(u, v);
}
}
}
} int ans = 0;
for (int u = 1; u <= n; u++)
ans = max(ans, longest_step(u));
cout << ans << '\n'; return 0;
}

Edit Step Ladders - UVA 10029的更多相关文章

  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

    題目:已知一些字典序排列的單詞,問能從中找到最大的一個有序單詞集合, 使得集合中的單詞每一個是有上一個單詞經過一次變換得來的(增.刪.改). 分析:dp,LIS.最大遞增子序列,不過數據較大须要優化. ...

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

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

  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. Go错误处理正确姿势

    1. panic 在什么情况下使用panic? 在程序启动的时候,如果有强依赖的服务出现故障时panic退出 在程序启动的时候,如果发现有配置明显不符合要求,可以panic退出(预防编程) 其他情况下 ...

  2. GO的GC辣鸡回收(一)

    用户程序通过内存分配器(Allocator)在堆上申请内存,而垃圾收集器(Collector)负责回收堆上的内存空间,内存分配器和垃圾收集器共同管理程序中的堆内存空间. 基本概念 垃圾分类 语义垃圾: ...

  3. 爱思助手备份 iPhone 时没有设置密码,恢复备份时需要密码的问题

    i4.cn 备份时 iPhone 上登陆的 Apple ID 曾经设置过备份密码,这个密码就是恢复备份时需要输入的密码!

  4. Metasploit用法详解

    Metasploit简介 1. Auxiliaries(辅助模块) 该模块不会直接在测试者和目标主机之间建立访问,它们只负责执行扫描.嗅探.指纹识别等相关功能以辅助渗透测试. 2. Exploit(漏 ...

  5. js基本数据类型之间的转换

    常见五大基本数据类型 1.number 2.string 3.boolean 4.undefined 5.null 一.转换为string ①调用toString() 方法 因为null和undefi ...

  6. volatile的基本原理

    volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...

  7. 【曹工杂谈】Maven源码调试工程搭建

    Maven源码调试工程搭建 思路 我们前面的文章<[曹工杂谈]Maven和Tomcat能有啥联系呢,都穿打补丁的衣服吗>分析了Maven大体的执行阶段,主要包括三个阶段: 启动类阶段,负责 ...

  8. Sentry-CLI 使用详解(2021 Sentry v21.8.x)

    内容源于:https://docs.sentry.io/platforms/javascript/guides/vue/ 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创 ...

  9. IS(上升子序列)

    前言:   这是一篇杂题选讲+作者口胡的博客,不喜勿喷. 正文:   提示:在阅读时请留意加粗的字体是"极长"还是"最长".   今天改题时碰到了一道关于线段树 ...

  10. Spring AOP框架 AspectJ

    1 AspectJ简介 v  AspectJ是一个基于Java语言的AOP框架 v  Spring2.0以后新增了对AspectJ切点表达式支持 v  @AspectJ 是AspectJ1.5新增功能 ...