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 ...
随机推荐
- springboot配置过滤器和拦截器
import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...
- C#格式化年月日截取
//if (bm.Name == "DateYear") //年 //{ // bm.Select(); ...
- 跳出双重for循环的案例__________跳出了,则不再执行标签ok下的for循环代码
ok: for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { System.out.print("*" ...
- .Net Core(二) 下
接上面 http://www.cnblogs.com/xcodevs/p/5584218.html 在解决方案浏览器中,右击 Controllers 目录.选择添加>新建项.选择Web API控 ...
- MVC微信浏览器图片上传(img转Base64)
因公司业务需要,需要做一个微信公众号里的图片上传功能,主要用到的技术就是 img转base64 上到服务器 话不多说, 贴代码 先看前端显示出来的东西 OK 图片不重要,看代码 <!--微信图片 ...
- 12.0&12.1 Xcode开发包
12.1开发包下载链接 12.0开发包下载链接 12.1(16B91)开发包下载链接 Finder打开后,按command+shift+G前往这个地址: /Applications/Xcode.app ...
- buf.readInt8函数详解
offset {Number} 0 noAssert {Boolean} 默认:false 返回:{Number} 从该 Buffer 指定的 offset 位置开始读取一个有符号的8位整数值. 设置 ...
- 安卓通过UDP协议传输数据,中文乱码的问题
公司最近需要往智能家居方面发展,需要用到UDP协议传输数据,在网上找到了一些资料,但是发现传输中文的时候有乱码的现象,经过我多番捣鼓,终于解决了这个问题,下面贴上关键代码 客户端: public cl ...
- fatal error C1083: 无法打开包括文件:“stdio.h
现象: vs2012一直fatal error C1083: 无法打开包括文件:"stdio.h" 不知道配置太多,动到了什么地方,出现了这个问题: 在: 解决方案--调试源文件 ...
- C# 判断字符串是否左包含
//测试字符串 左包含 //string str = "AAABBBCCC"; //char[] ss = str.ToArray(); //0-8 字符数组 //char[] s ...