[POJ 2001] Shortest Prefixes【Trie】

  • 2018-02-26
  • 0
  • 0

Problem:

Time Limit: 1000MS Memory Limit: 30000K

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

Rocky Mountain 2004

Solution:

这是 Trie (前缀树) 的一道模板题。

Trie 是将相同前缀储存在一起,以提高检索效率的一种树形结构

本题只需要用所有字符串建立一棵 Trie,统计每个节点前缀重复个数

对于每个字符串查询一次 Trie,找出对应的路径上第一个重复个数为 1 的前缀位置即可。

若无法找到,则其缩写为全串

注意 Trie 的空间开销较大,部分题目中可能会 MLE。

Code: O(NL), N为串数, L为串长 [976K, 16MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int n = 0;
char wrd[1002][22];
int len[1002];

struct Node{
	int cnt;
	Node *ch[26];
} trie[20002], *root;

inline Node* newNode(){
	static int topp = 0;
	return &trie[topp++];
}

inline void insert(char *wrd, int len){
	Node *cur = root;
	for(register int i = 0; i < len; i++){
		if(!cur->ch[wrd[i] - 'a']) cur->ch[wrd[i] - 'a'] = newNode();
		cur = cur->ch[wrd[i] - 'a'], cur->cnt++;
	}
}

inline int query(char *wrd, int len){
	Node *cur = root;
	for(register int i = 0; i < len; i++){
		cur = cur->ch[wrd[i] - 'a'];
		if(cur->cnt == 1) return i + 1;
	}
	return len;
}

int main(){
	while(scanf("%s", wrd[++n]) != EOF) len[n] = strlen(wrd[n]);
	n--, root = newNode();
	for(register int i = 1; i <= n; i++) insert(wrd[i], len[i]);
	for(register int i = 1; i <= n; i++){
		printf("%s", wrd[i]), putchar(' ');
		int abbr = query(wrd[i], len[i]);
		for(register int j = 0; j < abbr; j++) putchar(wrd[i][j]);
		putchar('\n');
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
If the code cannot be displayed normally, please refresh the page.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai