Shortest Prefixes-poj2001
链接:http://poj.org/problem?id=2001
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 <iostream> #include<cstdio> #include <string.h> using namespace std; char dic[1010][21]; struct Trie{ //字典树定义 Trie* next[26]; int num; //以当前字符串为前缀的单词的数量 Trie() //构造函数 { int i; for(i=0;i<26;i++){ next[i] = NULL; } num=0; } }; Trie root; void Insert(char word[]) //将字符串word插入到字典树中 { Trie *p = &root; int i; for(i=0;word[i];i++){ //遍历word的每一个字符 if(p->next[word[i]-'a']==NULL) //如果该字符没有对应的节点 p->next[word[i]-'a'] = new Trie; //创建一个,创建的同时就将该节点的值给赋好了 p = p->next[word[i]-'a'];//不管p->next[word[i]-'a']是否为空,都要执行这一步,对p进行更新,便于下一步操作 p->num++; } } int Find(char word[]) { Trie *p = &root; int i; for(i=0;word[i];i++){ //在字典树找到该单词中的未重复字母的起始位置 p = p->next[word[i]-'a']; //如果p->num>1,说明该字母后有多个分支,不是唯一的 if(p->num<=1)//由于需要的是最短前缀,所以从第一个字母开始遍历,如果找到某字母后面不再有分支,则停止查找 return i; } return -1;//如果该单词查找结束,发现全部字母的p->num均大于1,则该单词直接输出 } int main() { int count=0; while(~scanf("%s",dic[count])) Insert(dic[count++]); for (int j = 0; j < count; ++ j) { int ans = Find(dic[j]);//找到该单词中可区分前缀的初始位置 if (ans == -1) printf("%s %s\n", dic[j], dic[j]); else { printf("%s ", dic[j]); dic[j][ans + 1] = '\0';//把该单词余下的部分置为空,得出唯一的最短前缀 printf("%s\n", dic[j]); } } return 0; }