您的位置:首页 > 其它

成语接龙(Idiomatic Phrases Game)

2015-02-28 22:28 344 查看
Idiomatic Phrases Game

Problem Description

Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and
ends with the two given idioms. For every two adjacent idioms, the last Chinese character of the former idiom should be the same as the first character of the latter one. For each time, Tom has a dictionary that he must pick idioms from and each idiom in the
dictionary has a value indicates how long Tom will take to find the next proper idiom in the final list. Now you are asked to write a program to compute the shortest time Tom will take by giving you the idiom dictionary.

[align=left]Input[/align]
The input consists of several test cases. Each test case contains an idiom dictionary. The dictionary is started by an integer N (0 < N < 1000) in one line. The following is N lines. Each line contains an integer T (the time
Tom will take to work out) and an idiom. One idiom consists of several Chinese characters (at least 3) and one Chinese character consists of four hex digit (i.e., 0 to 9 and A to F). Note that the first and last idioms in the dictionary are the source and
target idioms in the game. The input ends up with a case that N = 0. Do not process this case.

[align=left]Output[/align]
One line for each case. Output an integer indicating the shortest time Tome will take. If the list can not be built, please output -1.

[align=left]Sample Input[/align]

5
5 12345978ABCD2341
5 23415608ACBD3412
7 34125678AEFD4123
15 23415673ACC34123
4 41235673FBCD2156
2
20 12345678ABCD
30 DCBF5432167D
0


[align=left]Sample Output[/align]

17
-1


成语接龙 分数:5

时间限制:1 秒
内存限制:32 兆
特殊判题:

标签

Dijkstra最短路径

题目描述

小明在玩成语接龙的游戏。成语接龙的规则是,如果成语A的最后一个汉字与成语B的第一个汉字相同,那么成语B就可以接到成语A的后面。

小明现在手上有一本成语词典,每次他都得花费一定时间来从当前的成语查到下一个可以接在后面的成语。

现在给你一个成语列表,请你计算从列表中的第一个成语开始,到接到列表中的最后一个成语最少需要多长时间。

输入格式

输入包含多组测试数据。

每组输入第一行是一个整数N(0<N<1000),表示成语列表的长度。

接下来N行,每行先输入一个整数T,再输入一个字符串S。

S表示一条成语,T表示小明从S查到下一个成语所花费的时间。

每条成语由至少3个汉字组成,每个汉字由4个十六进制数(0~9和A~F)组成。

当N=0时,输入结束。

输出

对于每组输入,输出从列表中的第一个成语开始,到接到列表中的最后一个成语需要的最少时间。

如果无法连接到列表中的最后一个成语,则输出-1。

样例输入

5

5 12345978ABCD2341

5 23415608ACBD3412

7 34125678AEFD4123

15 23415673ACC34123

4 41235673FBCD2156

2

20 12345678ABCD

30 DCBF5432167D

0

样例输出

17

-1

【分析】

这道题用到了图的知识与Dijkstra算法(单源最短路径)。

在这里附上来自于 海 子 的Dijkstra算法分析:点击打开链接

用java语言编写程序,代码如下:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(input.hasNext()) {
int n = input.nextInt();
if(n == 0) break;

int[] w = new int
;
String[] str = new String
;

for(int i = 0; i < n; i++) {
w[i] = input.nextInt();
str[i] = input.next();
}

//创建图——将字符串看成是顶点,而顶点的值是它所处的位置,而边则是两个能够匹配的字符串构成的“边”,
//边的权值则是从第一个字符串到另一个匹配字符串的时间
//g[i][j]表示从顶点i到顶点j所构成的边的权值,如果顶点i不能构成边,则权值赋值为最大值。
int[][] g = new int

;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
int len = str[i].length();
if(str[j].charAt(0) == str[i].charAt(len - 4) &&
str[j].charAt(1) == str[i].charAt(len - 3) &&
str[j].charAt(2) == str[i].charAt(len - 2) &&
str[j].charAt(3) == str[i].charAt(len - 1))
g[i][j] = w[i];
else
g[i][j] = Integer.MAX_VALUE;
}

int[] dist = new int
;
//单源最短路径算法
Dijkstra(n, g, dist);
if(dist[n - 1] != Integer.MAX_VALUE)
System.out.println(dist[n - 1]);
else
System.out.println(-1);
}
}

public static void Dijkstra(int n, int[][] g, int[] dist) {
boolean[] isVisited = new boolean
;
for(int i = 0; i < n; i++)
dist[i] = g[0][i];
dist[0] = 0;
isVisited[0] = true;
for(int i = 1; i < n; i++) {
int min = Integer.MAX_VALUE;
int u = 0;
for(int j = 0; j < n; j++) {
if(dist[j] < min && !isVisited[j]) {
min = dist[j];
u = j;
}
}

isVisited[u] = true;
for(int j = 0; j < n; j++) {
if(g[u][j] != Integer.MAX_VALUE && dist[u] + g[u][j] < dist[j] && !isVisited[j])
dist[j] = dist[u] + g[u][j];
}
}
}
}

另外这里还有一种时间超限的答案:(其中,同样是运用到了Dijkstra算法,只是实现不同罢了)

用java语言编写程序,代码如下:

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
//1124:成语接龙(时间超限)——Dijkstra最短路径

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(new BufferedInputStream(System.in));
while(input.hasNext()) {
int n = input.nextInt();
if(n == 0) break;

int source = 0, end = 0;
boolean[] isExist = new boolean[70000];
int numberOfVertices = 0;

PriorityQueue<WeightEdge>[] q = new PriorityQueue[70000];
for(int i = 0; i < n; i++) {
int t = input.nextInt();
String s = input.next();
int u = Integer.valueOf(s.substring(0, 4), 16);
int v = Integer.valueOf(s.substring(s.length() - 4, s.length()), 16);
WeightEdge edge = new WeightEdge(u, v, t);

if(i == 0)
source = u;
if(i == n - 1)
end = u;

if(isExist[u] == false) {
isExist[u] = true;
numberOfVertices++;
}

if(q[u] == null) {
q[u] = new PriorityQueue<WeightEdge>();
}
q[u].add(edge);
}

if(n == 1) {
System.out.println(0);
continue;
}
int[] dist = new int[70000];
getShortestPath(source, end, numberOfVertices, dist, q);
if(dist[end] == Integer.MAX_VALUE)
System.out.println(-1);
else
System.out.println(dist[end]);
}
}

static class WeightEdge implements Comparable<WeightEdge> {
int u, v, weight;
public WeightEdge(int u, int v, int weight) {
this.u = u;
this.v = v;
this.weight = weight;
}

public int compareTo(WeightEdge o) {
return (weight - o.weight);
}
}

public static void getShortestPath(int sourceIndex, int end, int numberOfVertices,
int[] dist, PriorityQueue<WeightEdge>[] queues) {
List<Integer> T = new ArrayList<Integer>();
T.add(sourceIndex);

for(int i = 0; i < dist.length; i++)
dist[i] = Integer.MAX_VALUE;
dist[sourceIndex] = 0;

while(T.size() < numberOfVertices) {
int v = -1;
int smallestDist = Integer.MAX_VALUE;
for(int u : T) {
if(queues[u] == null)
continue;

while(!queues[u].isEmpty() && T.contains(queues[u].peek().v))
queues[u].remove();

if(queues[u].isEmpty())
continue;

WeightEdge e = queues[u].peek();
if(dist[u] + e.weight < smallestDist) {
v = e.v;
smallestDist = dist[u] + e.weight;
}
}

if(v != -1) {
T.add(v);
dist[v] = smallestDist;
if(v == end)
return;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: