这道题一开始看的时候对于多组输入有点头疼,不知道如何将所有的字符串都建立在字典树上,后来想到Ctrl + Z可以跳出!= EOF的循环,就好做多了。
这题的思路是,先将所有的字符串都建立在字典树上,再进行遍历。遍历的条件就是,只是此字符串单独特有的前缀或者是这个字符串本身;通过flag的标记判断每个字母在不同的字符串中出现的次数,然后遍历节点找到flag==1的也就是说此乃字符串特有的前缀,或者是遍历一边最后指空的是前缀。
Description
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".
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".
Input
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.
Output
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.
Sample Input
carbohydrate cart carburetor caramel caribou carbonic cartilage carbon carriage carton car carbonate
Sample Output
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 <stdlib.h> #include <string.h> struct node { int flag; struct node* next[25]; }; struct node *root; char str1[1010][30] , str2[1010][30]; void build(char str1[]) { int k , i , j ,t; k = strlen(str1); struct node *p ; p = (struct node*) malloc (sizeof(struct node)); p = root; for(i = 0; i < k; i++) { t = str1[i] - 'a'; if(p -> next[t] != NULL) { (p -> next[t] -> flag) ++; p = p -> next[t]; } else { p -> next[t] = (struct node*) malloc (sizeof(struct node)); for(j = 0; j < 26; j++) { p -> next[t] -> next[j] = NULL; } p -> next[t] -> flag = 0; p = p -> next[t]; } } } void find(char str1[] , char str2[]) { int k , i , t; k = strlen(str1); struct node*p; p = (struct node*) malloc (sizeof(struct node)); p = root; for(i = 0; i < k; i++) { t = str1[i] - 'a'; p = p -> next[t]; str2[i] = str1[i]; if(p -> flag == 0) { i++; str2[i] = str1[i]; str2[i++] = '\0'; return ; } } str2[i] = '\0'; } int main() { int i; int j; root = (struct node*)malloc (sizeof(struct node)); for(i = 0; i < 26; i++) { root -> next[i] = NULL; } i = 0; while(scanf("%s", str1[i]) != EOF) { build(str1[i]); i++; } for(j = 0 ; j < i; j++) { find(str1[j] , str2[j]); } for(j = 0; j < i; j++) { printf("%s %s\n", str1[j] , str2[j]); } return 0; }