首页 > 其他分享 >成语接龙(Idiomatic Phrases Game)

成语接龙(Idiomatic Phrases Game)

时间:2022-11-28 20:02:57浏览次数:38  
标签:dist int Phrases Idiomatic 接龙 str input new 成语


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.  

Input

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.  

Output

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.

Sample Input

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

Sample Output

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[n];
String[] str = new String[n];

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[n][n];
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[n];
//单源最短路径算法
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[n];
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;
}
}
}
}





标签:dist,int,Phrases,Idiomatic,接龙,str,input,new,成语
From: https://blog.51cto.com/u_15894233/5893614

相关文章

  • SPOJ PHRASES Relevant Phrases of Annihilation
    DescriptionYouaretheKingofByteland.Youragentshavejustinterceptedabatchofencryptedenemymessagesconcerningthedateoftheplannedattackon......
  • P1688 新单词接龙问题 题解
    大家好,我喜欢Hash,所以我用Hash通过了这道题。思路:处理方法有点类似于P6521首先我们最终接出的字符串要求其中的小字符串按字典序排序,那么我们就将所有字符串排好序......
  • R语言编程——用不到30行代码,自制成语接龙小游戏
    ​中国文化底蕴深厚,在漫长的历史中,我们的语言文字形成了大量特定的组合,用来表达特定的意思。成语是中国传统文化的一大特色,有的来源于典故,有的则来源于固定的使用方法。在成......
  • [NOIP2000 提高组] 单词接龙
    [NOIP2000提高组]单词接龙题目背景注意:本题为上古NOIP原题,不保证存在靠谱的做法能通过该数据范围下的所有数据。题目描述单词接龙是一个与我们经常玩的成语接龙相......
  • 【搜索】力扣126:单词接龙 II(过于hard)
    给定一个起始字符串和一个终止字符串,以及一个单词表,求是否可以将起始字符串每次改一个字符,直到改成终止字符串,且所有中间的修改过程表示的字符串都可以在单词表里找到。若......
  • [AcWing 1117] 单词接龙
    DFS点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=50+10;intn;stringword[N];intg[N][N];//g[i][......
  • 汪子熙趣味成语接龙的游戏软件设计架构说明
    @目录背景战士阿短编程猫纸片初始化函数当开始被点击当收到广播“转盘停止”当收到广播“开始接龙”本作品采用Kitten编程猫v3.7.11开发而成。工程里主要包含一个背景......
  • 趣味成语接龙游戏里,如何判断用户输入的成语接龙成功?
    本文给出了一种解决方案,采用如下的kitten积木组合块实现。根据变量“检查接龙的返回值”,分别执行相应的逻辑。如果返回值为-1,说明用户输入的词语长度不为4.如果返回......
  • 汪子熙趣味成语接龙游戏的设计初衷
    我国的汉语博大精深,其中数以万计的四字成语更是汉语中一颗颗璀璨的明珠,凝聚着中华民族几千年文明的精华。从小接触这些成语,对于小学生积累语汇,提高文学素养,和学习文言文方......
  • 使用 Kitten 开发一款趣味成语接龙游戏
    每一轮接龙成功后,初始接龙和成功接龙的成语,都会显示在作品的接龙记录里,便于使用者学习和记忆。通过积分的方式,能激励用户开动脑筋,努力完成接龙。本作品极具智能和体贴性,如......