UVA - 10029 Edit Step Ladders (二分+hash)
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)的更多相关文章
- UVa 10029 - Edit Step Ladders
題目:已知一些字典序排列的單詞,問能從中找到最大的一個有序單詞集合, 使得集合中的單詞每一個是有上一個單詞經過一次變換得來的(增.刪.改). 分析:dp,LIS.最大遞增子序列,不過數據較大须要優化. ...
- 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 - 12338 Anti-Rhyme Pairs 二分+hash
题目链接:传送门 题意: 给你n个串 问你任意两个串的最大公共前缀长度是多少 题解: 二分+hash 思路很明显,我最近用来写hash 很鸡肋 #include<bits/stdc++.h> ...
- UVA12206 Stammering Aliens 【SAM 或 二分 + hash】
题意 求一个串中出现至少m次的子串的最大长度,对于最大长度,求出最大的左端点 题解 本来想练哈希的,没忍住就写了一个SAM SAM拿来做就很裸了 只要检查每个节点的right集合大小是否不小于m,然后 ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)
题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...
随机推荐
- Python 35 线程(2)线程特性、守护线程、线程互斥锁
一:线程特性介绍 from threading import Thread import time n=100 def task(): global n n=0 if __name__ == '__m ...
- 离线安装 Rancher2.2.4 HA 集群
一.先决条件(所有主机执行) 1.1 基础设置 1.安装基础软件 yum install -y vim net-tools wget lrzsz 2.防火墙 sed -i 's/SELINUX=enf ...
- 制作一个 JavaScript 小游戏
简评: 作者学习了编程两个月,边学边做了一个 JavaScript 小游戏,在文中总结了自己在这个过程中的一些体会,希望能给其他初学者一些帮助. 对于很多想学编程但一直没下定决心的同学来说,最大的问题 ...
- Android检测代理
1. System.getProperties().remove("http.proxyHost"); System.getProperties().remove("ht ...
- Super超级ERP系统---(9)订单管理--订单拣货
订单审核完成后,库房就可以开始备货,安排相应的人员去拣货了.订单拣货主要分为一次分拣和二次分拣,这里我们先看下一次分拣的流程.一次分拣就是根据订单去拣货,可以简单的理解为拿着一个订单,推着购物车,把当 ...
- C#url相关知识
C#中Url地址重定向的方法: 1:Response.Redirect(url); 这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但他可以跳转到任何页面,没有站点页面限 ...
- ES6 Template String 模板字符串
模板字符串(Template String)是增强版的字符串,用反引号(`)标识,它可以当作普通字符串使用,也可以用来定义多行字符串,或者在字符串中嵌入变量. 大家可以先看下面一段代码: $(&quo ...
- jQuery中容易让人困惑的东西
前言:jqueryt很灵活,太灵活了,可以说是他一个优点,也是他一个缺点,达到一种效果,十个人也许会用十种不同的方法来实现这个过程,结果一样,过程不一样,这到底是好,还是坏呢. 一,什么是jquery ...
- CNN:Windows下编译使用Caffe和Caffe2
用于检测的CNN分为基于回归网络的方法和基于区域+CNN网络的方法,其中基于回归网络的方法典型为YOLO9000,可以兼容使用VGG-Net框架.其中基于区域+CNN网络方法,大量使用了Caffe作为 ...
- (转)openlayers实现在线编辑
http://blog.csdn.net/gisshixisheng/article/details/46054949 概述: 在前面有篇博文讲述了基于Arcgis for js和wkt实现在线数据的 ...