码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 2564 Edit Step Ladders

时间:2014-05-07 23:01:04      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   ext   int   

题意:题目按字典序给你多个字符串,判断如果一个字符串通过加,减,变一个字母的情况下可以变成另一个字符串的话,就代表他们之间有一个阶梯,求最多的阶梯

思路:首先我们应该想到这个有点LIS的感觉,然后我们可以采用记忆化搜索,然后就是每当一个字符串进行相应的变化后就去查找后面是否有这个字符串,依次找下去,判断最大值,重点是要通过HASH来优化

#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 = 0;
	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+1);
	return 0;
}



POJ - 2564 Edit Step Ladders,布布扣,bubuko.com

POJ - 2564 Edit Step Ladders

标签:style   blog   class   code   ext   int   

原文地址:http://blog.csdn.net/u011345136/article/details/25226693

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!