题意

题目链接(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. Sublime Text3 显示左侧的目录树

    file->open folder选择一个文件夹,打开一个新窗口把原来的关掉 View->Sise Bar->Hide Side Bar就可以了

  2. Mysql 中隐式转换

    案例一:条件字段函数操作 假设你现在维护了一个交易系统,其中交易记录表 tradelog 包含交易流水号(tradeid).交易员 id(operator).交易时间(t_modified)等字段.为 ...

  3. Java 大数加法HdAcm1002

    1 import java.util.Scanner; 2 3 4 public class Main { 5 public static void main(String[] args) { 6 S ...

  4. Python和java的选择

    它是什么? Java是一种通用的面向对象的编程语言,主要用于开发从移动应用程序到Web到企业应用程序的各种应用程序. Python是一种高级的面向对象的编程语言,主要用于Web开发,人工智能,机器学习 ...

  5. Java通过网络图片之地址,下载到服务器

    @RequestMapping("/downloadTableQrcode") public String downloadTableQrcode(HttpServletReque ...

  6. dotnet C# 给结构体字段赋值非线程安全

    在 dotnet 运行时中,给引用对象进行赋值替换的时候,是线程安全的.给结构体对象赋值,如果此结构体是某个类的成员字段,那么此赋值不一定是线程安全的.是否线程安全,取决于结构体的大小,取决于此结构体 ...

  7. Centos7最小化系统安装_配置

    本文总结了作者使用centos最小化安装时,碰到的问题和解决方案. 网络问题.作者使用虚拟机安装时,网卡并没有激活.操作: 1 cd /etc/sysconfig/network-script 2 v ...

  8. MapperScannerConfigurer之sqlSessionFactoryBeanName注入方式

    Spring整合Mybatis时,项目启动时报错:(MapperScannerConfigurer之sqlSessionFactoryBeanName注入方式) pringframework.bean ...

  9. vue实现拖动div元素

    html: <div id="app1"> <div v-drag class="drag"></div> <div ...

  10. React项目中应用TypeScript

    一.前言 单独的使用typescript 并不会导致学习成本很高,但是绝大部分前端开发者的项目都是依赖于框架的 例如和vue.react 这些框架结合使用的时候,会有一定的门槛 使用 TypeScri ...