UVA - 10029 Edit Step Ladders (二分+hash)

Description

Download as PDF

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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值