poj 2001 Shortest Prefixes(字典树 )

20 篇文章 0 订阅
5 篇文章 0 订阅
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 14619 Accepted: 6310

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

Source


题目大意:给你很多个字符串,让你找每个字符串的最短前缀,但这么多字符串前缀不能有相同的,要能唯一标识一个字符串,而且前缀长度尽量小。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector> 
#include<algorithm>
using namespace std;

struct node
{
  int x;
  node *next[26];
  node()
  {
  	x=0;
  	memset(next,0,sizeof(next));
  }
};
node *root=NULL;

void insert(char *s)
{
	node *p=root;
	node *p1=NULL;
	int i,k;
	for(i=0;s[i];i++)
	{
		k=s[i]-'a';
		if(p->next[k]==NULL)
		 {
		 	p1=new node;
		 	p->next[k]=p1;
		 }
		 else
	       p->next[k]->x++;
	    p=p->next[k];
	}
}

void search(char *s)
{
	node *p=root;
	int i,k;
	for(i=0;s[i];i++)
	{
	    k=s[i]-'a';
		if(p->next[k]==NULL)
		 return ;
		 p=p->next[k];
		 printf("%c",s[i]);
		 if(p->x==0)
		   break;
	}
}

char str[1010][25];
int main()
{
  int i=0;
  root=new node;
  
  while(~scanf("%s",str[i]))
  {
  	insert(str[i]);
  	i++;
  }
  
  for(int j=0;j<i;j++)
  {
  	printf("%s ",str[j]);
  	search(str[j]);
  	printf("\n");
  }
  return 0;	
} 


人家的代码
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include<stdlib.h>  
  4. const int MAX=26;  
  5. char str[1000][21],res[1000][21];  
  6. struct node{      
  7.     struct node *next[MAX]; //字母分支   
  8.     int flag; //字母出线过的次数统计  
  9. }node;  
  10. struct node *root; //根节点  
  11. void inset_tree(char *str) //插入字符串  
  12. {   
  13.     int ans,i,len;   
  14.     struct node *p,*q;   
  15.     p=root; //p先指向根节点      
  16.     len=strlen(str);    
  17.     for(i=0;i<len;i++){    
  18.         ans=str[i]-'a';  //每一个字母的字母编号    
  19.         if(p->next[ans]!=NULL)  //如果非空    
  20.         {     
  21.             p=p->next[ans];  //p指向该字母节点     
  22.             p->flag++; //该字母出现过的次数加1    
  23.         }    
  24.         else{     
  25.             q=(struct node*)calloc(1,sizeof(node)); //创建新节点     
  26.             p->next[ans]=q;  //q赋给该节点     
  27.             p=q;   //p指向该节点     
  28.             p->flag=1; //节点数为1    
  29.         }   
  30.     }  
  31. }  
  32. void find(char *str,char *res) //查找  
  33. {   
  34.     int len,ans,i;   
  35.     struct node *p;   
  36.     p=root;   
  37.     len=strlen(str);   
  38.     for(i=0;i<len;i++){    
  39.         ans=str[i]-'a';    
  40.         p=p->next[ans]; //p指向该字母节点    
  41.         res[i]=str[i]; //该字母赋给结果数组    
  42.         if(p->flag==1) //如果该字母是这个字符串所独有的    
  43.         {     
  44.             res[++i]='\0'//查找完成     
  45.             return ;    
  46.         }   
  47.     }  
  48. }  
  49. int main(){   
  50.     int n=0,i;   
  51.     root=(struct node *)calloc(1,sizeof(node));   
  52.     while(scanf("%s",str[n])!=EOF) //插入输入的每一个字符串   
  53.     {    
  54.         inset_tree(str[n]);    
  55.         n++;   
  56.     }   
  57.     for(i=0;i<n;i++) //查找每一个字符串的最短有效前缀    
  58.         find(str[i],res[i]);   
  59.     for(i=0;i<n;i++)    
  60.         printf("%s %s\n",str[i],res[i]);   
  61.     return 0;  
  62. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值