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
解题思路
这题字典树用指针做的,看的userluoxuan的博客,嘻嘻嘻,发现指针真是个神奇的东西,还有结构体;
结构体中包含下一个节点的指针,详情请见代码
AC代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn = 26;
char word[1005][30];
struct Trie
{
int n;
Trie *next[maxn];
};
Trie *root; //定义结构体指针,最上面的根节点
void init()
{
root = (Trie *)malloc(sizeof(Trie)); //初始化,malloc是分配空间的
for(int i = 0; i < maxn; i++)
root -> next[i] = NULL; //根节点的26个分指针,初始化为空
}
void insert(char *word)
{
Trie *temp = root;
for(int i = 0; i < strlen(word); i++)
{
int pos = word[i] - 'a';
if(temp -> next[pos] == NULL) //如果代表这条路径的指针为空
{
Trie *cur = (Trie *)malloc(sizeof(Trie)); //定义需新建的指针
cur -> n = 1; //n记录经过的次数
for(int j = 0; j < maxn; j++)
cur -> next[j] = NULL;
temp -> next[pos] = cur;
}
else
temp -> next[pos] -> n++; //如果这条路径已存在,n自增
temp = temp -> next[pos]; //temp指针向下传递
}
}
void search(char *word)
{
Trie *temp = root;
char ans[25];
for(int i = 0; i < strlen(word); i++)
{
temp = temp -> next[ word[i] - 'a' ];
ans[i] = word[i];
ans[i + 1] = '\0';
if(temp -> n == 1) //到只经过一次的独有字母能够做前缀
{
printf("%s %s\n", word, ans);
return ;
}
}
printf("%s %s\n", word, ans); //或者整个单词做前缀
return ;
}
int main()
{
int count = 0;
init();
while(scanf("%s", word[++count]) != EOF) //输入EOF后count已经自增了,所以下面是 < 号
insert(word[count]);
for(int i = 1; i < count; i++)
search(word[i]);
return 0;
}
未AC代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
int trie[1010][27], val[1010];
int num_jd;
void init()
{
num_jd = 1;
memset(trie, 0, sizeof(trie));
memset(val, 0, sizeof(val));
}
void insert(char *s)
{
int u = 0, i, c, len = strlen(s);
for(i = 0; i < len; i++)
{
c = s[i] - 'a';
if(!trie[u][c])
trie[u][c] = num_jd++;
u = trie[u][c];
val[u]++;
}
}
void query(char *s)
{
int u = 0, i, j, c, len = strlen(s);
for(i = 0; i < len; i++)
{
c = s[i] - 'a';
u = trie[u][c];
if(val[u] == 1 || i == len - 1)
{
printf("%s ", s);
s[i + 1] = '\0';
printf("%s\n", s);
return ;
}
}
}
int main()
{
char str[1010][30];
int h, i, num_str;
init();
for(h = 0; ; h++)
{
if(scanf("%s", str[h]) != EOF)
insert(str[h]);
else
break;
}
num_str = h;
for(i = 0; i < num_str; i++)
{
query(str[i]);
}
}