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 ...
随机推荐
- SpringCloud学习 什么是微服务(一)
关于SpringCloud,我是看了周老师的<SpringCloud与Docker微服务架构实战>之后才有了一点了解,做下记录,以供后期学习.本人知识有限,如有不对,欢迎批评 1.什么是单 ...
- ASP之ViewState和IsPostBack
没怎么写过ASPX页面,今天在做增删改的界面的时候,修改出了问题. 根据传过来的ObjectID加载页面数据,赋值给TextBox控件后,修改控件的值回写数据库,发现值没有变化. 简单的例子如下: 然 ...
- 虚拟机下不能运行gazebo
bug描述: VMware: vmw_ioctl_command error Invalid argument. 解决方式:设置环境变量 export SVGA_VGPU10=0 或者 echo &q ...
- .net core2.0 中使用aspectcore实现aop
一.新建一个web application项目 1.1.添加AspectCore.Extensions.DependencyInjection引用 二.实现AbstractInterceptorAtt ...
- CDN 内容分发网络
第一步,HTML的文件引用:HTML的文件头(也有文件中,文件尾)那边常有其他文件引用,比如CSS以及JS的引用. 就以bootstrap常用的引用来举个栗子你常见的引用可能会是这样的: <he ...
- inline-block默认间距解决方法
方法一: 父元素设置font-size: 0; 行内块元素有文字时再在该元素上设置font-size 方法二: 父元素设置word-spacing为负 方法三: Inline-block 元素浮 ...
- 三维重建:SFM中BA的并行化
1. BA在重建中的作用 借鉴于运动中重建的方法,BA引入SLAM过程,而传统的滤波方法引入BA是跟随闭环检测出现. 1.1 BA在滤波方法中的嵌入 PTAM 1.2 BA在闭环检测之后的应用 在三维 ...
- Spring AOP之静态代理
软件151 李飞瑶 一.SpringAOP: ⒈AOP:Aspect Oriented Programming 面向切面编程, 实现的是核心业务和非核心业务之间的的分离,让核心类只做核心业务,代理类只 ...
- OpenWRT 常用软件安装
root@Jack:/tmp/opkg-lists# opkg--help opkg:unrecognized option `--help' opkgmust have one sub-comman ...
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题解: 方法一: 搞一个KDtree, ...