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 ...
随机推荐
- maven添加本地jar包的方法
1.将一个本地的jar包随便放在一个放入本地文件夹中 (文件夹位置 和 jar包名称都随意) 例:F:\java\repository\a 文件夹下,名称为:icepdf-core-6.0.jar 2 ...
- U - Three displays
Problem description It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk ( ...
- Vue 区别
computed和methods区别 效果是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值. 而methods,在重新渲染的时候,函数总会重新调用执行.
- 仿QQ空间长图效果简易版--母亲节感恩
手机网站 母亲节最火的两件事 1.NBA 杜兰特在获MVP催泪致辞献给母亲:她才是真的MVP. 2.QQ空间长图 ------------------------------------------- ...
- 【转】linux命令
shell实例手册 0 说明{ 手册制作: 雪松 更新日期: 2015-11-02 欢迎系统运维加入Q群: 198173206 # 加群请回答问题 欢迎运维开发加入Q群: 3655344 ...
- C++程序设计实验安排
2016-2017第二学期C++程序设计的实验时间与地点安排如下表,请大家根据时间按时来上机实验.另外,因为原来安排在4.1的实验因为调休补周一的课,因此挪至周五.另外第4次周六的课,考虑有一些同学有 ...
- THREE.js代码备份——webgl - materials - cube refraction [balls](以上下左右前后6张图片构成立体场景、透明球体效果)
<!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - ma ...
- What is the difference between PKCS#5 padding and PKCS#7 padding
The difference between the PKCS#5 and PKCS#7 padding mechanisms is the block size; PKCS#5 padding is ...
- Webpack 快速上手(下)
杏仁前端开发工程师,代码洁癖症早期,关注前端技术. 由于文章篇幅较长,为了更好的阅读体验,本文分为上.中.下三篇: 上篇介绍了什么是 webpack,为什么需要 webpack,webpack 的文件 ...
- PAT_A1140#Look-and-say Sequence
Source: PAT A1140 Look-and-say Sequence (20 分) Description: Look-and-say sequence is a sequence of i ...