【POJ】2001 - Shortest Prefixes(字典树)

来源:互联网 发布:法剧 知乎 编辑:程序博客网 时间:2024/04/23 14:26

点击打开题目

Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 17357 Accepted: 7551

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". 

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

carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonate

Sample Output

carbohydrate carbohcart cartcarburetor carbucaramel caracaribou caricarbonic carbonicartilage carticarbon carboncarriage carrcarton cartocar carcarbonate carbona

Source

Rocky Mountain 2004



估计再写两道题我的字典树风格就确定了,个人感觉字典树还是要用灵活,不是每个变量都有固定的含义,要随机应变。


代码如下:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define CLR(a,b) memset(a,b,sizeof(a))#define INF 0x3f3f3f3fstruct Trie{Trie *next[26];int v;void init(){v = 0;for (int i = 0 ; i < 26 ; i++)next[i] = NULL;}};Trie *root;int idx(char c){return c - 'a';}void insert(char s[][22],int x){int l = strlen(s[x]);Trie *p = root , *q;for (int i = 0 ; i < l ; i++){int id = idx(s[x][i]);if (p->next[id] == NULL){q = (Trie *)malloc(sizeof(Trie));//开新指针 q->init();p->next[id] = q;}p = p->next[id];p->v++;}}void min_permix(char s[][22],int x)//查询最小前缀 {printf ("%s ",s[x]);int l = strlen(s[x]);Trie *p = root;for (int i = 0 ; i < l ; i++){printf ("%c",s[x][i]);int id = idx(s[x][i]);p = p->next[id];if (p->v == 1)//如果字母单独出现说明已经出现最小前缀了,结束break;}printf ("\n");}int main(){char s[1011][22];int ant = 0;root = (Trie *)malloc(sizeof(Trie));root->init();while (~scanf ("%s",s[ant++]))insert(s,ant-1);for (int i = 0 ; i < ant ; i++)min_permix(s , i);return 0;}



0 0
原创粉丝点击