关闭

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

147人阅读 评论(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网站的观点或立场

POJ 2001 字典树裸题

求每个单词最短不重复前缀 #include #include #include #include #include #include ...
  • qq574857122
  • qq574857122
  • 2013-10-03 19:04
  • 1693

lintcode_单词切分

给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词。 您在真实的面试中是否遇到过这个题?  Yes 样例 给出 ...
  • weilianyishi
  • weilianyishi
  • 2016-05-10 20:52
  • 1867

Oracle数据字典 和 查询表中字段的注释方法

Java 获取 表的属性 可以通过ResultSetMetaData接口那么 有没有办法 获取表中某个字段的 注释? select comments   from USER_COL_COMM...
  • yaov_yy
  • yaov_yy
  • 2013-06-24 10:26
  • 2509

POJ_2001_Shortest Prefixes(字典树)

题意:求给出的每个单词的“标志前缀”,即此前缀只能在此单词中有,而其他单词不拥有,所以叫做标志前缀。
  • jhgkjhg_ugtdk77
  • jhgkjhg_ugtdk77
  • 2015-03-29 23:46
  • 492

Shortest Prefixes (POJ_2001) 字典树

Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16582   Accepte...
  • Keeping111
  • Keeping111
  • 2016-03-09 21:09
  • 200

POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepte...
  • ACMore_Xiong
  • ACMore_Xiong
  • 2015-08-06 17:11
  • 1338

【POJ 2001】Shortest Prefixes(字典树)

Shortest PrefixesTime Limit: 1000MS Memory Limit: 30000KDescriptionA prefix of a string is a su...
  • thesprit
  • thesprit
  • 2016-08-08 15:30
  • 395

POJ 2001 Shortest Prefixes ( 字典树 )

题意:给出n个单词(1 分析:字典树的基础应用。 Trie树(字典树),又称单词查找树或键树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常...
  • u010862260
  • u010862260
  • 2013-10-04 22:36
  • 463

【POJ 2001 Shortest Prefixes】+ 字典树

Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17793 Acc...
  • WYK1823376647
  • WYK1823376647
  • 2016-12-02 22:44
  • 162

POJ 2001 Shortest Prefixes (字典树)

题目类型  字典树 题目意思 给出最多 1000 个最长 20 的字符串 问每个字符串可以准确识别的最短识别长度对应的字符串是什么 准确识别长度的意思是如果前这个长度的字符都匹配...
  • Yunyouxi
  • Yunyouxi
  • 2015-03-18 21:58
  • 244
    时刻记住--never give up
    人生如棋,吾愿为卒,吾行虽慢,谁曾见吾后退半步!
    个人资料
    • 访问:194140次
    • 积分:8851
    • 等级:
    • 排名:第2500名
    • 原创:723篇
    • 转载:3篇
    • 译文:0篇
    • 评论:27条