关闭

POJ 2001 Shortest Prefixes (字典树模拟切割)

116人阅读 评论(0) 收藏 举报
分类:
Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16059   Accepted: 6929

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



题意:一堆字符串,求每个串的辨识前缀

思路:模拟了切割字符串,枚举切割点,查看前缀出现的次数,如果出现了一次,则说明这个前缀是辨识前缀,直接
输出,如果查找到最后仍没有查出来,就输出原串

ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stack>
#include<iostream>
#include<algorithm>
#define fab(a) (a)>0?(a):(-a)
#define LL long long
#define MAXN 55000
#define mem(x) memset(x,0,sizeof(x))
#define INF 0xfffffff 
using namespace std;
struct s
{
	int num;
	s *next[26];
};
s *root;
char ss[MAXN][255];
void create(char *str)
{
	int len=strlen(str);
	s *p=root,*q;
	for(int i=0;i<len;i++)
	{
		int id=str[i]-'a';
		if(p->next[id]==NULL)
		{
			q=(s *)malloc(sizeof(s));
			q->num=1;
			for(int j=0;j<26;j++)
			q->next[j]=NULL;
			p->next[id]=q;
			p=p->next[id];
		}
		else
		{
			p->next[id]->num++; 
			p=p->next[id];
		}
	}
	//p->num=1;
}
int find(char *ss)
{
	int len=strlen(ss);
	s *p=root;
	for(int i=0;i<len;i++)
	{
		int id=ss[i]-'a';
		p=p->next[id];
		if(p==NULL)
		return 0;
	}
	return p->num;
}
void begin()
{
	for(int i=0;i<26;i++)
	root->next[i]=NULL;
//	root->num=0;
}
void freetree(s *t)
{
	if(t==NULL)
	return;
	for(int i=0;i<26;i++)
	{
		if(t->next[i]!=NULL)
		freetree(t->next[i]);
	}
	free(t);
	return;
}
int main()
{
	root=(s *)malloc(sizeof(s));
	begin();
	int k=0;
	char ne1[260],ne2[260];
	while(scanf("%s",ss[k])!=EOF)
	{
		create(ss[k]);
		k++;
	}
	for(int i=0;i<k;i++)
	{
		int len=strlen(ss[i]);
		int bz=0;
		for(int j=1;j<len;j++)
		{
		    int qq=0;
			for(int q=0;q<j;q++)
			ne1[qq++]=ss[i][q];
			ne1[qq]='\0';
			if(find(ne1)==1)
			{
				bz=1;
				printf("%s %s\n",ss[i],ne1);
				break;
			}
		}
		if(bz==0)
		printf("%s %s\n",ss[i],ss[i]);
	}
	return 0;
}


 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    时刻记住--never give up
    人生如棋,吾愿为卒,吾行虽慢,谁曾见吾后退半步!
    个人资料
    • 访问:141871次
    • 积分:8264
    • 等级:
    • 排名:第2384名
    • 原创:717篇
    • 转载:3篇
    • 译文:0篇
    • 评论:26条