Shortest Prefixes
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 21651 | Accepted: 9277 |
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
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
题意:输出每个单词以及它的独有前缀;
分析:将输入单词建树,记录每个节点访问次数,建树完成后枚举每个单词的节点并输出节
点对应的字母,找到访问次数为1的节点则停止查询该单词
数组实现
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int t[15000][26];
char ss[1001][21];
int num[15000],pos=1;
void insert(char *s)
{
int x,rt=0;
for(int i=0;s[i];i++)
{
x=s[i]-'a';
if(!t[rt][x])
t[rt][x]=pos++;
rt=t[rt][x];
num[rt]++;
}
}
void find(char *s)
{
int x,rt=0;
for(int i=0;s[i];i++)
{
x=s[i]-'a';
printf("%c", s[i]);
rt=t[rt][x];
if(num[rt]==1)
return ;
}
}
int main()
{
int c=0;
while(~scanf("%s",ss[c]))
insert(ss[c++]);
for(int i=0;i<c;i++)
{
printf("%s ",ss[i]);
find(ss[i]);
printf("\n");
}
return 0;
}
指针实现
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char ss[1001][21];
struct node
{
int vis;
struct node *next[26];
node()
{
vis=0;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*rt;
void insert(char *s)
{
node *p=rt;
for(int i=0;s[i];i++)
{
int x=s[i]-'a';
if(!p->next[x])
p->next[x]=new node;
p=p->next[x];
p->vis++;//访问次数增加
}
}
void find(char *s)
{
node *p=rt;
for(int i=0;s[i];i++)
{
printf("%c",s[i]);
int x=s[i]-'a';
p=p->next[x];
if(p->vis==1)//访问一次的就是独有的
return ;
}
}
int main()
{
rt=new node;
int c=0;
while(scanf("%s",ss[c])!=EOF)
insert(ss[c++]);
for(int i=0;i<c;i++)
{
printf("%s ",ss[i]);
find(ss[i]);
printf("\n");
}
return 0;
}