Shortest Prefixes
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 8118 | Accepted: 3445 |
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.
今天看了看TIRE的模板,顺手改了改把这题A了。。。。
#include<stdio.h>
#include<string.h>
#define max 26
#define maxn1 1001
#define maxn2 30
char as[maxn1][maxn2],tt[maxn2];
struct node//初始tire树
{
int num,k;//标记是否为相同前缀最后字符
node *next[max];//后继结点
} * root;
void insert(char ss[])//插入操作
{
int i,j;
node *p=root,*t;//p指向当前节点,t用来开辟内存
for(i=0;ss[i];i++)//将每个字符存起来的过程
{
if(p->next[ss[i]-'a']==NULL)//当p指向为空的时候,需要开辟新的节点
{
t=new node;
t->k = t->num = 0;
for(j=0;j<max;j++)
t->next[j]=NULL;
p->next[ss[i]-'a']=t;//接上新的节点
}
p->k++;
p=p->next[ss[i]-'a'];//p指向下一个节点
p->num++;//
}
}
void search(char ss[])//查找
{
node *p=root;//p指向根节点
int t=0;
for(int i=0;ss[i];i++)
{
p=p->next[ss[i]-'a'];//p指向下一结点
if(p->num>1)
{
tt[t]=ss[i];
t++;
}
if(p->num==1)
{
tt[t]=ss[i];
tt[t+1]='\0';
return;
}
}
if(p->k == 1) tt[t]='\0';
tt[t]='\0';//这个是防止上面的p->num都是大于1的
return ;
}
int main()
{
int l = 0,i,j;
char s[maxn2];
root = new node;
root->k = root->num = 0;
for(j=0;j<max;j++)
root->next[j]=NULL;
while(scanf("%s",s) != EOF) {
//if (s[0] == '0') break;
strcpy(as[l++],s);
insert(s);
}
for(i = 0;i < l;i++) {
search(as[i]);
printf("%s %s\n",as[i],tt);
}
//scanf("%d",&i);
return 0;
}