Doublets
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1090 Accepted: 371 Special Judge

Description

A Doublet is a pair of words that differ in exactly one letter; for example, “booster” and “rooster” or “rooster” and “roaster” or “roaster” and “roasted”.
You are given a dictionary of up to 25143 lower case words, not exceeding 16 letters each. You are then given a number of pairs of words. For each pair of words, find the shortest sequence of words that begins with the first word and ends with the second, such that each pair of adjacent words is a doublet. For example, if you were given the input pair “booster” and “roasted”, a possible solution would be: (“booster”, “rooster”, “roaster”, “roasted”) provided that these words are all in the dictionary.

Input

Input consists of the dictionary followed by a number of word pairs. The dictionary consists of a number of words, one per line, and is terminated by an empty line. The pairs of input words follow; the words of each pair occur on a line separated by a space.

Output

For each input pair, print a set of lines starting with the first word and ending with the last. Each pair of adjacent lines must be a doublet. If there are several minimal solutions, any one will do. If there is no solution, print a line: “No solution.” Leave a blank line between cases.

Sample Input

booster
rooster
roaster
coasted
roasted
coastal
postal

booster roasted
coastal postal

Sample Output

booster
rooster
roaster
roasted

No solution.

Source
Waterloo local 1999.01.31

问题链接:UVA10150 POJ2647 Doublets
问题简述:定义两个单词为doublet序列,当两个序列的长度相同,且只有一个字母不相同,给出一个单词库,计算两个单词之间可以通过doublet序列达到的路径。
问题分析:用BFS实现。
程序说明:代码在POJ中出现了TLE,程序需要改进。程序代码中使用了goto语言来跳出多重循环。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10150 POJ2647 Doublets */#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <cstring>using namespace std;vector<string> dict;
map<string, int> pos;
const int N = 25143;
int next2[N];
string s, t;void bfs()
{memset(next2, -1, sizeof(next2));int ids = pos[s];int idt = pos[t];next2[idt] = -2;queue<int> q;q.push(idt);while(!q.empty()) {int u = q.front();q.pop();string s2 = dict[u];for(int i = 0; s2[i]; i++)for(int c = 'a'; c <= 'z'; c++) {string t2 = s2;t2[i] = c;if(t2 != s2) {if(pos.find(t2) != pos.end()) {int v = pos[t2];if(next2[v] == -1) {next2[v] = u;if(v == ids) goto stop;q.push(v);}}}}}stop:if(next2[ids] == -1) cout << "No solution." << endl;elsefor(int i = ids; i != -2; i = next2[i]) cout << dict[i] << endl;
}int main()
{int cnt = 0;string word;while(getline(cin, word) && word != "" ) {pos[word] = cnt++;dict.push_back(word);}int flag = 0;while(cin >> s >> t) {if(flag) cout << endl;else flag = 1;if(pos.find(s) == pos.end()) cout << "No solution." << endl;else if(pos.find(t) == pos.end()) cout << "No solution." << endl;else bfs();}return 0;
}

UVA10150 POJ2647 Doublets【BFS】相关推荐

  1. 【BFS】魔板(c++基础算法)

    本专栏上一篇:[BFS]八数码问题(c++基础算法) 目录 一.读题 ①题面 ②题意 三.做题 ①算法原理 ②算法实现 Ⅰ三种基本操作 Ⅱ操作序列 Ⅲ输出 Ⅳ一个特殊情况 四.AC代码 最后 一.读题 ...

  2. POJ 3414 Pots【BFS】+ Python

    原题链接: 3414 -- Pots 参考资料:POJ 3414 - Pots | 眈眈探求 POJ 3414 Pots[BFS][图搜] - it610.com 一 特别注意: 1. 每一种操作对应 ...

  3. 【枚举】【二分答案】【分块答案】【BFS】【最大流】【Dinic】bzoj1189 [HNOI2007]紧急疏散evacuate...

    [法一]枚举Time(0~N*M): S->'.'(1); 'D'->T(Time); '.'->'D'(dis(用BFS预处理,注意一旦到达'D',BFS就不能继续扩展了,注意di ...

  4. nyoj 284 坦克大战【bfs】

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" i ...

  5. [kuangbin]专题二 搜索进阶 Escape HDU - 3533【BFS】

    [题目描述] The students of the HEU are maneuvering for their military training. The red army and the blu ...

  6. 【BFS】献给阿尔吉侬的花束(C++)

    [题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...

  7. 【BFS】天棋哥哥大战AlphaGo 校OJ2395

    题目描述 3月15日,人机围棋大战巅峰对决在韩国首尔落下帷幕.五番棋的最后一局中,韩国著名棋手李世乭尽管与人工智能"AlphaGo"缠斗至官子阶段,但在双双进入读秒后最终还是投子认 ...

  8. 【bfs】WJ的逃离

    WJ(J)的逃离 题目大意: 有一个n×m的矩阵,*是不可走的,0是可走的,求1,1到n,m的最小转弯次数 原题: 题目描述 当WJ醒来时,发现自己被困在一个地图的左上角,幸好WJ有张图,并了解到出口 ...

  9. Bailian4129 变换的迷宫【BFS】

    4129:变换的迷宫 总时间限制: 1000ms 内存限制: 65536kB 描述 你现在身处一个R*C 的迷宫中,你的位置用"S" 表示,迷宫的出口用"E" ...

最新文章

  1. 用python画关系网络图-使用python画社交网络图实例代码
  2. prototype.js常用函数及其用法
  3. 七人のオンラインゲーマーズ 全年龄正式汉化补丁
  4. Netty 服务 如何 接收新的连接
  5. WinRunner介绍
  6. 微信公众号开发踩坑指南(1)——服务器验证与Token获取失败原因
  7. php开发我的世界插件,[搬运插件] [服务端插件] [管理]PlotMe——地皮插件[1.2.5-1.10.2]...
  8. python 基于numpy的线性代数运算
  9. xmos固件u8_XU208 USB数字界面 XMOS U8升级版 模块_便宜推
  10. 计算机视觉(ComputerVision, CV)相关领域的网站链接
  11. C#代码审计实战+前置知识
  12. strlen、strcpy、strcmp、strcat函数的实现
  13. GOROOT 和 GOPATH 的区别
  14. MS---数据库概念回顾
  15. 不会PS图片怎么批量调色
  16. 我在华为写了13年的代码
  17. OpenGL的图形渲染过程
  18. php 处理小语种字符,小语种语言信息处理 人工智能领域的开拓者
  19. ngrok穿透内网(内网有代理的情况)
  20. Android如何把一个活动设置成主活动

热门文章

  1. 基于flash AS3.0 的BASE64编码与解码类
  2. 高级着色语言HLSL入门(1)
  3. 【数据结构的魅力】008.图
  4. 安装Ubuntu下的开发工具
  5. HBuilderX真机调试检测不到魅族手机
  6. 表单绑定复选框的值和图片上传
  7. Error in callback for watcher “value“: “TypeError: Cannot read property ‘repalce‘ of null“
  8. 从服务端接收数组_Kafka系列第6篇:消息是如何在服务端存储与读取的,你真的知道吗?...
  9. 地理信息系统概论_2021考研专业课地理信息系统概论(黄杏元版)知识点总结(五)...
  10. switch语句训练