Description

Problem C: Edit Step Ladders

An edit step is a transformation from one word x to another word
y such that x and y are words in the dictionary, and
x
can be transformed to y by adding, deleting, or changing one letter. So the transformation from
dig to dog or from dog to do are both edit steps. An
edit step ladder is a lexicographically ordered sequence of words w1, w2, ... wn such that the transformation from
wi to wi+1 is an edit step for all i from 1 to
n-1.

For a given dictionary, you are to compute the length of the longest edit step ladder.

Input

The input to your program consists of the dictionary - a set of lower case words in lexicographic order - one per line. No word exceeds 16 letters and there are no more than 25000 words in the dictionary.

Output

The output consists of a single integer, the number of words in the longest edit step ladder.

Sample Input

cat
dig
dog
fig
fin
fine
fog
log
wine

Sample Output

5
题意:给你一个递增的字符串数组,给你三种操作方法变成其它的串,问你最长的可能
思路:hash+二分,dp[i]表示从i開始的串的最长变化可能。记忆化搜索
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 25010;
const int HASH = 1000010; int n, head[HASH], next[MAXN], f[MAXN];
char b[MAXN][20], temp[20]; int hash(char *s) {
int v = 0,seed = 131;
while (*s)
v = v * seed + *(s++);
return (v & 0x7fffffff) % HASH;
} void insert(int s) {
int h = hash(b[s]);
next[s] = head[h];
head[h] = s;
} int search(char *s) {
int i,h = hash(s);
for (i = head[h]; i != -1; i = next[i])
if (!strcmp(b[i],s))
break;
return i;
} void add(char *s, int p, int d) {
int i = 0, j = 0;
while (i < p)
temp[j++] = s[i++];
temp[j++] = 'a' + d;
while (s[i])
temp[j++] = s[i++];
temp[j] = '\0';
} void del(char *s, int p) {
int i = 0,j = 0;
while (i < p)
temp[j++] = s[i++];
i++;
while (s[i])
temp[j++] = s[i++];
temp[j] = '\0';
} void change(char *s, int p, int d) {
strcpy(temp, s);
temp[p] = 'a' + d;
} int dp(int s) {
if (f[s] != -1)
return f[s];
int ans = 1;
int len = strlen(b[s]);
for (int p = 0; p <= len; p++)
for (int d = 0; d < 26; d++) {
add(b[s], p, d);
int v = search(temp);
if (v != -1 && strcmp(b[s], temp) < 0){
int t = dp(v);
if (ans < t+1)
ans = t+1;
}
}
for (int p = 0; p < len; p++) {
del(b[s], p);
int v = search(temp);
if (v != -1 && strcmp(b[s], temp) < 0) {
int t = dp(v);
if (ans < t+1)
ans = t+1;
}
}
for (int p = 0; p < len; p++)
for (int d = 0; d < 26; d++) {
change(b[s], p, d);
int v = search(temp);
if (v != -1 && strcmp(b[s], temp) < 0) {
int t = dp(v);
if (ans < t+1)
ans = t+1;
}
}
return f[s] = ans;
} int main() {
n = 0;
memset(head, -1, sizeof(head));
while (scanf("%s", b[n]) != EOF) {
insert(n),++n;
}
memset(f, -1, sizeof(f));
int ans = 0;
for (int i = 0; i < n; i++) {
int t = dp(i);
if (ans < t)
ans = t;
}
printf("%d\n", ans);
return 0;
}

UVA - 10029 Edit Step Ladders (二分+hash)的更多相关文章

  1. UVa 10029 - Edit Step Ladders

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

  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 - 12338 Anti-Rhyme Pairs 二分+hash

    题目链接:传送门 题意: 给你n个串 问你任意两个串的最大公共前缀长度是多少 题解: 二分+hash 思路很明显,我最近用来写hash 很鸡肋 #include<bits/stdc++.h> ...

  7. UVA12206 Stammering Aliens 【SAM 或 二分 + hash】

    题意 求一个串中出现至少m次的子串的最大长度,对于最大长度,求出最大的左端点 题解 本来想练哈希的,没忍住就写了一个SAM SAM拿来做就很裸了 只要检查每个节点的right集合大小是否不小于m,然后 ...

  8. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

  9. BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)

    题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...

随机推荐

  1. 在linux系统中,使用tomcat的shutdown.sh脚本停止应用,但是进程还在的解决办法

    基本原理为启动tomcat时记录启动tomcat的进程id(pid),关闭时强制杀死该进程 第一步 :vi 修改tomcat下bin/catalina.sh文件,增加几行脚本,主要是记录tomcat的 ...

  2. dom转换成jquery对象

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  3. Spring 的优秀工具类盘点---转

    第 1 部分: 文件资源操作和 Web 相关工具类 http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/ 文件资源操作 文件资源的 ...

  4. WPF TextBox 仅允许输入数字

    因为在 IValueConverter 实现中,当文本不能转换为目标类型时返回 DependencyProperty.UnsetValue ,Validation.GetHasError 返回 tru ...

  5. android黑科技系列——防自动抢红包外挂原理解析

    一.前言 春节过年发个红包本来就是为了讨个喜庆,朋友亲戚之间的关系交流,但是现在随着技术变革,抢红包插件越来越多,导致现在不太愿意发红包了,特别是在一个多人群里,潜水的非常多,但是丢个红包瞬间就没了, ...

  6. halcon 模板匹配 -- create_shape_model

    create_shape_model(Template : : //reduce_domain后的模板图像 NumLevels,//金字塔的层数,可设为“auto”或0—10的整数 AngleStar ...

  7. 5G vs AI谁更有前途?

    5G vs AI谁更有前途? 5G通信技术和AI人工智能技术是两个不同层面的技术领域,而它们两者都将在未来20年内对世界的发展有着革命性和里程碑式的影响.未来5G和AI谁更有前途呢? 5G技术的发展和 ...

  8. Linux下文件查找命令find笔记

    在Linux命令下如果需要快速自己系统所需要处理的文件,可以通过find命令快速进行检索. 如果想在某个路径下查找相应的文件可以执行如下命令: find path -name filename # p ...

  9. es 存入失败错误

    {, 'error': {'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [bd_date] ...

  10. 04-Linux系统编程-第01天(文件IO、阻塞非阻塞)

    03-系统函数 系统编程章节大纲 1 文件I/O 2 文件系统 3 进程 4 进程间通信 5 信号 6 进程间关系 7 守护进程 8 线程 9 线程同步 10 网络基础 11 socket编程 12 ...