SOJ 2325:Word Transformation _floyd

A common word puzzle found in many newspapers and magazines is the word transformation. By taking a starting word and successively altering a single letter to make a new word, one can build a sequence of words which changes the original word to a given end word. For instance, the word ``spice'' can be transformed in four steps to the word ``stock'' according to the following sequence: spice, slice, slick, stick, stock. Each successive word differs from the previous word in only a single character position while the word length remains the same.

Given a dictionary of words from which to make transformations, plus a list of starting and ending words, your team is to write a program to determine the number of steps in the shortest possible transformation.

The input will be a single file in two sections. The first section will be the dictionary of available words with one word per line, terminated by a line containing an asterisk (*) rather than a word. There can be up to 200 words in the dictionary; all words will be in lower case, and no word will be longer than ten characters an no two words are the same. Words can appear in the dictionary in any order.

Following the dictionary are pairs of words, one pair per line, with the words in the pair separated by a single space. These pairs represent the starting and ending words ( possibly the same )in a transformation. The pairs are terminated by the end-of-file. If it's impossible ,output"Impossible". Also words are both in the dictionary .

The output should contain one line per word pair, and must include the starting word, the ending word, and the number of steps in the shortest possible transformation, separated by single spaces.

(For those people who listen to National Public Radio (NPR), the Sunday morning broadcast Weekend Edition, Sunday usually has a word puzzle for the listening audience. Occasionally the puzzle is a transformation. NPR allows the submission of puzzle answers via e-mail, to the address wesun@npr.org. You can use this code to generate solutions to the transformation puzzles and possibly get a chance to be on the air for the live puzzle round.)


Sample Input
dip
lip
mad
map
maple
may
pad
pip
pod
pop
sap
sip
slice
slick
spice
stick
stock
*
spice stock
may pod

Sample Output
spice stock 4
may pod 3


Source:

Southern California Regional of the ACM International Collegiate Programming Contest


//川大个人赛上的一道题,题意是利用一开始给出的字典(“*”前面的所有字符串就是字典里面的),查找给出的两个字符串("*"后面的)第一个要经过多少次变换能够得到第二个字符串。变换过程需满足:1、这两个字符长度相同;2、每次只能变换其中一个字符;3、每一次变换得到的字符串都必须在之前给的字典里面。

//分析:由于要求每次变换过程中要求 中介 字符串都必须在字典里面,换个想法就是说我们将每个字符串看成一个点,那么某个字符串(点)变换(走)的过程中也必须在这些点上面,也就相当于选择一条最短路径了。 那么可以预先将字典里面两两字符串之间需要变换的次数算出来,最后进行最短路。题目后有多次访问,而字符串个数最多才200个,所以选择floyd最佳。 需要注意的是:题目要求每次 只能变换字符串中的一个字符,所有只有两个字符串(长度相同)只有一个字符不同时才建立单位1的路径,否则为0xfffffff。

#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
using namespace std;
#define  maxn 205
#define inf 0xfffffff

map<string,int>my;
int dis[maxn][maxn];
int Get_Len(char *a,char *b)
{
	int len=strlen(a),ans=0,i;
	for(i=0;i<len;i++)
		if(a[i]!=b[i])
			ans++;
	return ans>1?inf:1;  //注意
}

int main()
{
	char ss[205][15];
	char st[15],en[15];
	int n=0,i,j,k;
	for(i=0;i<maxn;i++)
		for(j=0;j<maxn;j++)
		{
			if(i==j)
				dis[i][j]=dis[j][i]=0;
			else
				dis[i][j]=dis[j][i]=inf;
		}
	my.clear();
	while(~scanf("%s",st))
	{
		if(st[0]!='*')
		{
			my[string(st)]=n;
			strcpy(ss[n],st);
			n++;
			for(i=0;i<n;i++)
				for(j=i+1;j<n;j++)
				{
					if(strlen(ss[i])!=strlen(ss[j]))
						continue;
					dis[i][j]=dis[j][i]=Get_Len(ss[i],ss[j]);
				}
			continue;
		}
		for(k=0;k<n;k++)
			for(i=0;i<n;i++)
				for(j=0;j<n;j++)
					if(dis[i][j]>dis[i][k]+dis[k][j])
						dis[i][j]=dis[i][k]+dis[k][j];
		int x,y;				
		while(~scanf("%s%s",st,en))
		{
			x=my[string(st)],y=my[string(en)];
			if(dis[x][y]<inf)
			{
				printf("%s %s %d\n",st,en,dis[x][y]);
				continue;
			}
			puts("Impossible");
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值