Shortest Prefixes 最短前缀(c语言)

题目描述
A prefix of a string is a substring starting at the beginning of the given string. The prefixes of “carbon” are: “c”, “ca”, “car”, “carb”, “carbo”, and “carbon”. Note that the empty string is not considered a prefix in this problem, but every non-empty string is considered to be a prefix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, “carbohydrate” is commonly abbreviated by “carb”. In this problem, given a set of words, you will find for each word the shortest prefix that uniquely identifies the word it represents.

In the sample input below, “carbohydrate” can be abbreviated to “carboh”, but it cannot be abbreviated to “carbo” (or anything shorter) because there are other words in the list that begin with “carbo”.

An exact match will override a prefix match. For example, the prefix “car” matches the given word “car” exactly. Therefore, it is understood without ambiguity that “car” is an abbreviation for “car” , not for “carriage” or any of the other words in the list that begins with “car”.
字符串的前缀是从给定字符串的开头开始的子字符串。“碳”的前缀是:“c”,“ca”,“car”,“carb”,“carbo”和“carbon”。请注意,在此问题中,空字符串不被视为前缀,但每个非空字符串都被视为其自身的前缀。在日常语言中,我们倾向于用前缀来缩写单词。例如,“碳水化合物”通常缩写为“碳水化合物”。在此问题中,给定一组单词,您将为每个单词找到唯一标识其所表示的单词的最短前缀。

在下面的示例输入中,“碳水化合物”可以缩写为“carboh”,但不能缩写为“carbo”(或任何更短的东西),因为列表中还有其他单词以“carbo”开头。

完全匹配将覆盖前缀匹配。例如,前缀“car”与给定的单词“car”完全匹配。因此,可以毫不含糊地理解,“car”是“car”的缩写,而不是“carriage”或列表中以“car”开头的任何其他单词。
输入
The input contains at least two, but no more than 1000 lines. Each line contains one word consisting of 1 to 20 lower case letters.
输入至少包含两行,但不超过 1000 行。每行包含一个由 1 到 20 个小写字母组成的单词。
输出
The output contains the same number of lines as the input. Each line of the output contains the word from the corresponding line of the input, followed by one blank space, and the shortest prefix that uniquely (without ambiguity) identifies this word.
输出包含与输入相同的行数。输出的每一行都包含输入相应行中的单词,后跟一个空格,以及唯一(无歧义)标识此单词的最短前缀。

样例输入
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
样例输出
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
char a[1002][25];
int s=0,t=0,cb[30001][26],ca[30001];
void insert(char ch[])
{
	int w=0,l=strlen(ch+1),p=1,k;
	while(p<=l)
	   {ca[w]++;
	    k=ch[p]-'a';
	    if(!cb[w][k]) cb[w][k]=++s;
	    w=cb[w][k]; p++;
	   }
	ca[w]++;
}
void find(char ch[])
{
	int w=0,l=strlen(ch+1),p=1,k;
	while(p<=l)
	   {if(ca[w]==1&&w!=0) break;
	    k=ch[p]-'a';
	    printf("%c",ch[p]);
	    w=cb[w][k]; p++;
	   }
}
int main()
{
	while(scanf("%s",a[++t]+1)!=EOF) insert(a[t]); 
	for(int i=1;i<=t;i++)
	   {printf("%s ",a[i]+1);
	    find(a[i]);
	    printf("\n");
	   }
	return 0;
}
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
公交车最短路径是指在给定的公交线路网络中,找到乘客从起点到终点的最短距离的路径。为了解决这个问题,可以使用C语言来实现Dijkstra算法来找到最短路径。Dijkstra算法是一种用于解决图形中单源最短路径问题的贪心算法。下面是一个用C语言实现的简单示例: ```c #include <stdio.h> #define INF 99999 void dijkstra(int graph[5][5], int source, int destination){ int dist[5], visited[5], count, min, nextnode; for(int i = 0; i < 5; i++){ dist[i] = graph[source][i]; visited[i] = 0; } dist[source] = 0; visited[source] = 1; count = 1; while(count < 5 - 1){ min = INF; for(int i = 0; i < 5; i++){ if(dist[i] < min && !visited[i]){ min = dist[i]; nextnode = i; } } visited[nextnode] = 1; for(int i = 0; i < 5; i++){ if(!visited[i]){ if(min + graph[nextnode][i] < dist[i]){ dist[i] = min + graph[nextnode][i]; } } } count++; } printf("The shortest path from %d to %d is: %d", source, destination, dist[destination]); } int main(){ int graph[5][5] = {{0, 1, 4, 0, 0}, {1, 0, 4, 2, 7}, {4, 4, 0, 3, 0}, {0, 2, 3, 0, 4}, {0, 7, 0, 4, 0}}; int source = 0, destination = 4; dijkstra(graph, source, destination); return 0; } ``` 这段代码首先定义了一个5x5的邻接矩阵来表示公交线路网络,然后使用Dijkstra算法来计算从起点到终点的最短路径。在这个简单的例子中,起点为0,终点为4,最短路径的距离会被打印出来。在实际开发中,可以根据具体需求进行定制,以实现公交车最短路径的计算和展示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值